blob: b4ac9119200e6bbba5e6cd086139c8d4dbcae476 [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>
24#include <linux/vfs.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080025#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mount.h>
27#include <linux/namei.h>
28#include <linux/quotaops.h>
29
30struct file_system_type reiserfs_fs_type;
31
32static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
33static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
34static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
35
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070036int is_reiserfs_3_5(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070038 return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
39 strlen(reiserfs_3_5_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070042int is_reiserfs_3_6(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070044 return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
45 strlen(reiserfs_3_6_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048int is_reiserfs_jr(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070050 return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
51 strlen(reiserfs_jr_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070054static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070056 return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
57 is_reiserfs_jr(rs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060static int reiserfs_remount(struct super_block *s, int *flags, char *data);
David Howells726c3342006-06-23 02:02:58 -070061static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063static int reiserfs_sync_fs(struct super_block *s, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065 if (!(s->s_flags & MS_RDONLY)) {
66 struct reiserfs_transaction_handle th;
67 reiserfs_write_lock(s);
68 if (!journal_begin(&th, s, 1))
69 if (!journal_end_sync(&th, s, 1))
70 reiserfs_flush_old_commits(s);
71 s->s_dirt = 0; /* Even if it's not true.
72 * We'll loop forever in sync_supers otherwise */
73 reiserfs_write_unlock(s);
74 } else {
75 s->s_dirt = 0;
76 }
77 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static void reiserfs_write_super(struct super_block *s)
81{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070082 reiserfs_sync_fs(s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085static void reiserfs_write_super_lockfs(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070087 struct reiserfs_transaction_handle th;
88 reiserfs_write_lock(s);
89 if (!(s->s_flags & MS_RDONLY)) {
90 int err = journal_begin(&th, s, 1);
91 if (err) {
92 reiserfs_block_writes(&th);
93 } else {
94 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
95 1);
96 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
97 reiserfs_block_writes(&th);
98 journal_end_sync(&th, s, 1);
99 }
100 }
101 s->s_dirt = 0;
102 reiserfs_write_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700105static void reiserfs_unlockfs(struct super_block *s)
106{
107 reiserfs_allow_writes(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700110extern const struct in_core_key MAX_IN_CORE_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* this is used to delete "save link" when there are no items of a
113 file it points to. It can either happen if unlink is completed but
114 "save unlink" removal, or if file has both unlink and truncate
115 pending and as unlink completes first (because key of "save link"
116 protecting unlink is bigger that a key lf "save link" which
117 protects truncate), so there left no items to make truncate
118 completion on */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700119static int remove_save_link_only(struct super_block *s,
120 struct reiserfs_key *key, int oid_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700122 struct reiserfs_transaction_handle th;
123 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125 /* we are going to do one balancing */
126 err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
127 if (err)
128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700130 reiserfs_delete_solid_item(&th, NULL, key);
131 if (oid_free)
132 /* removals are protected by direct items */
133 reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));
134
135 return journal_end(&th, s, JOURNAL_PER_BALANCE_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#ifdef CONFIG_QUOTA
139static int reiserfs_quota_on_mount(struct super_block *, int);
140#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* look for uncompleted unlinks and truncates and complete them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143static int finish_unfinished(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145 INITIALIZE_PATH(path);
146 struct cpu_key max_cpu_key, obj_key;
147 struct reiserfs_key save_link_key;
148 int retval = 0;
149 struct item_head *ih;
150 struct buffer_head *bh;
151 int item_pos;
152 char *item;
153 int done;
154 struct inode *inode;
155 int truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700157 int i;
158 int ms_active_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700160
161 /* compose key to look for "save" links */
162 max_cpu_key.version = KEY_FORMAT_3_5;
163 max_cpu_key.on_disk_key.k_dir_id = ~0U;
164 max_cpu_key.on_disk_key.k_objectid = ~0U;
165 set_cpu_key_k_offset(&max_cpu_key, ~0U);
166 max_cpu_key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169 /* Needed for iput() to work correctly and not trash data */
170 if (s->s_flags & MS_ACTIVE) {
171 ms_active_set = 0;
172 } else {
173 ms_active_set = 1;
174 s->s_flags |= MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 /* Turn on quotas so that they are updated correctly */
177 for (i = 0; i < MAXQUOTAS; i++) {
178 if (REISERFS_SB(s)->s_qf_names[i]) {
179 int ret = reiserfs_quota_on_mount(s, i);
180 if (ret < 0)
181 reiserfs_warning(s,
182 "reiserfs: cannot turn on journalled quota: error %d",
183 ret);
184 }
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188 done = 0;
189 REISERFS_SB(s)->s_is_unlinked_ok = 1;
190 while (!retval) {
191 retval = search_item(s, &max_cpu_key, &path);
192 if (retval != ITEM_NOT_FOUND) {
193 reiserfs_warning(s,
194 "vs-2140: finish_unfinished: search_by_key returned %d",
195 retval);
196 break;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 bh = get_last_bh(&path);
200 item_pos = get_item_pos(&path);
201 if (item_pos != B_NR_ITEMS(bh)) {
202 reiserfs_warning(s,
203 "vs-2060: finish_unfinished: wrong position found");
204 break;
205 }
206 item_pos--;
207 ih = B_N_PITEM_HEAD(bh, item_pos);
208
209 if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
210 /* there are no "save" links anymore */
211 break;
212
213 save_link_key = ih->ih_key;
214 if (is_indirect_le_ih(ih))
215 truncate = 1;
216 else
217 truncate = 0;
218
219 /* reiserfs_iget needs k_dirid and k_objectid only */
220 item = B_I_PITEM(bh, ih);
221 obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
222 obj_key.on_disk_key.k_objectid =
223 le32_to_cpu(ih->ih_key.k_objectid);
224 obj_key.on_disk_key.k_offset = 0;
225 obj_key.on_disk_key.k_type = 0;
226
227 pathrelse(&path);
228
229 inode = reiserfs_iget(s, &obj_key);
230 if (!inode) {
231 /* the unlink almost completed, it just did not manage to remove
232 "save" link and release objectid */
233 reiserfs_warning(s,
234 "vs-2180: finish_unfinished: iget failed for %K",
235 &obj_key);
236 retval = remove_save_link_only(s, &save_link_key, 1);
237 continue;
238 }
239
240 if (!truncate && inode->i_nlink) {
241 /* file is not unlinked */
242 reiserfs_warning(s,
243 "vs-2185: finish_unfinished: file %K is not unlinked",
244 &obj_key);
245 retval = remove_save_link_only(s, &save_link_key, 0);
246 continue;
247 }
248 DQUOT_INIT(inode);
249
250 if (truncate && S_ISDIR(inode->i_mode)) {
251 /* We got a truncate request for a dir which is impossible.
252 The only imaginable way is to execute unfinished truncate request
253 then boot into old kernel, remove the file and create dir with
254 the same key. */
255 reiserfs_warning(s,
256 "green-2101: impossible truncate on a directory %k. Please report",
257 INODE_PKEY(inode));
258 retval = remove_save_link_only(s, &save_link_key, 0);
259 truncate = 0;
260 iput(inode);
261 continue;
262 }
263
264 if (truncate) {
265 REISERFS_I(inode)->i_flags |=
266 i_link_saved_truncate_mask;
267 /* not completed truncate found. New size was committed together
268 with "save" link */
269 reiserfs_info(s, "Truncating %k to %Ld ..",
270 INODE_PKEY(inode), inode->i_size);
271 reiserfs_truncate_file(inode,
272 0
273 /*don't update modification time */
274 );
275 retval = remove_save_link(inode, truncate);
276 } else {
277 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
278 /* not completed unlink (rmdir) found */
279 reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
280 /* removal gets completed in iput */
281 retval = 0;
282 }
283
284 iput(inode);
285 printk("done\n");
286 done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288 REISERFS_SB(s)->s_is_unlinked_ok = 0;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 /* Turn quotas off */
292 for (i = 0; i < MAXQUOTAS; i++) {
293 if (sb_dqopt(s)->files[i])
294 vfs_quota_off_mount(s, i);
295 }
296 if (ms_active_set)
297 /* Restore the flag back */
298 s->s_flags &= ~MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700300 pathrelse(&path);
301 if (done)
302 reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
303 "Completed\n", done);
304 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/* to protect file being unlinked from getting lost we "safe" link files
308 being unlinked. This link will be deleted in the same transaction with last
309 item of file. mounting the filesytem we scan all these links and remove
310 files which almost got lost */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700311void add_save_link(struct reiserfs_transaction_handle *th,
312 struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 INITIALIZE_PATH(path);
315 int retval;
316 struct cpu_key key;
317 struct item_head ih;
318 __le32 link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700320 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700322 /* file can only get one "save link" of each kind */
323 RFALSE(truncate &&
324 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
325 "saved link already exists for truncated inode %lx",
326 (long)inode->i_ino);
327 RFALSE(!truncate &&
328 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
329 "saved link already exists for unlinked inode %lx",
330 (long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700332 /* setup key of "save" link */
333 key.version = KEY_FORMAT_3_5;
334 key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
335 key.on_disk_key.k_objectid = inode->i_ino;
336 if (!truncate) {
337 /* unlink, rmdir, rename */
338 set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
339 set_cpu_key_k_type(&key, TYPE_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700341 /* item head of "safe" link */
342 make_le_item_head(&ih, &key, key.version,
343 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
344 4 /*length */ , 0xffff /*free space */ );
345 } else {
346 /* truncate */
347 if (S_ISDIR(inode->i_mode))
348 reiserfs_warning(inode->i_sb,
349 "green-2102: Adding a truncate savelink for a directory %k! Please report",
350 INODE_PKEY(inode));
351 set_cpu_key_k_offset(&key, 1);
352 set_cpu_key_k_type(&key, TYPE_INDIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354 /* item head of "safe" link */
355 make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
356 4 /*length */ , 0 /*free space */ );
357 }
358 key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700360 /* look for its place in the tree */
361 retval = search_item(inode->i_sb, &key, &path);
362 if (retval != ITEM_NOT_FOUND) {
363 if (retval != -ENOSPC)
364 reiserfs_warning(inode->i_sb, "vs-2100: add_save_link:"
365 "search_by_key (%K) returned %d", &key,
366 retval);
367 pathrelse(&path);
368 return;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700371 /* body of "save" link */
372 link = INODE_PKEY(inode)->k_dir_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374 /* put "save" link inot tree, don't charge quota to anyone */
375 retval =
376 reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
377 if (retval) {
378 if (retval != -ENOSPC)
379 reiserfs_warning(inode->i_sb,
380 "vs-2120: add_save_link: insert_item returned %d",
381 retval);
382 } else {
383 if (truncate)
384 REISERFS_I(inode)->i_flags |=
385 i_link_saved_truncate_mask;
386 else
387 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/* this opens transaction unlike add_save_link */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700392int remove_save_link(struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 struct reiserfs_transaction_handle th;
395 struct reiserfs_key key;
396 int err;
397
398 /* we are going to do one balancing only */
399 err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
400 if (err)
401 return err;
402
403 /* setup key of "save" link */
404 key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
405 key.k_objectid = INODE_PKEY(inode)->k_objectid;
406 if (!truncate) {
407 /* unlink, rmdir, rename */
408 set_le_key_k_offset(KEY_FORMAT_3_5, &key,
409 1 + inode->i_sb->s_blocksize);
410 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
411 } else {
412 /* truncate */
413 set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
414 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
415 }
416
417 if ((truncate &&
418 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
419 (!truncate &&
420 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
421 /* don't take quota bytes from anywhere */
422 reiserfs_delete_solid_item(&th, NULL, &key);
423 if (!truncate) {
424 reiserfs_release_objectid(&th, inode->i_ino);
425 REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
426 } else
427 REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
428
429 return journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
David Howellsedc666e2006-10-11 01:22:14 -0700432static void reiserfs_kill_sb(struct super_block *s)
433{
434 if (REISERFS_SB(s)) {
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -0700435#ifdef CONFIG_REISERFS_FS_XATTR
David Howellsedc666e2006-10-11 01:22:14 -0700436 if (REISERFS_SB(s)->xattr_root) {
437 d_invalidate(REISERFS_SB(s)->xattr_root);
438 dput(REISERFS_SB(s)->xattr_root);
439 REISERFS_SB(s)->xattr_root = NULL;
440 }
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -0700441#endif
David Howellsedc666e2006-10-11 01:22:14 -0700442 if (REISERFS_SB(s)->priv_root) {
443 d_invalidate(REISERFS_SB(s)->priv_root);
444 dput(REISERFS_SB(s)->priv_root);
445 REISERFS_SB(s)->priv_root = NULL;
446 }
447 }
448
449 kill_block_super(s);
450}
451
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700452static void reiserfs_put_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700454 struct reiserfs_transaction_handle th;
455 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700457 /* change file system state to current state if it was mounted with read-write permissions */
458 if (!(s->s_flags & MS_RDONLY)) {
459 if (!journal_begin(&th, s, 10)) {
460 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
461 1);
462 set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
463 REISERFS_SB(s)->s_mount_state);
464 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
465 }
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700468 /* note, journal_release checks for readonly mount, and can decide not
469 ** to do a journal_end
470 */
471 journal_release(&th, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Jeff Mahoney50652272006-09-30 23:28:44 -0700473 reiserfs_free_bitmap_cache(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700475 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477 print_statistics(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700479 if (REISERFS_SB(s)->reserved_blocks != 0) {
480 reiserfs_warning(s,
481 "green-2005: reiserfs_put_super: reserved blocks left %d",
482 REISERFS_SB(s)->reserved_blocks);
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700485 reiserfs_proc_info_done(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487 kfree(s->s_fs_info);
488 s->s_fs_info = NULL;
489
490 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Christoph Lametere18b8902006-12-06 20:33:20 -0800493static struct kmem_cache *reiserfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495static struct inode *reiserfs_alloc_inode(struct super_block *sb)
496{
497 struct reiserfs_inode_info *ei;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700498 ei = (struct reiserfs_inode_info *)
Christoph Lametere94b1762006-12-06 20:33:17 -0800499 kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!ei)
501 return NULL;
502 return &ei->vfs_inode;
503}
504
505static void reiserfs_destroy_inode(struct inode *inode)
506{
507 kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
508}
509
Christoph Lametere18b8902006-12-06 20:33:20 -0800510static void init_once(void *foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700512 struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Christoph Lametera35afb82007-05-16 22:10:57 -0700514 INIT_LIST_HEAD(&ei->i_prealloc_list);
515 inode_init_once(&ei->vfs_inode);
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700516#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Christoph Lametera35afb82007-05-16 22:10:57 -0700517 ei->i_acl_access = NULL;
518 ei->i_acl_default = NULL;
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700519#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static int init_inodecache(void)
523{
524 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700525 sizeof(struct
526 reiserfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800527 0, (SLAB_RECLAIM_ACCOUNT|
528 SLAB_MEM_SPREAD),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700529 init_once, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (reiserfs_inode_cachep == NULL)
531 return -ENOMEM;
532 return 0;
533}
534
535static void destroy_inodecache(void)
536{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700537 kmem_cache_destroy(reiserfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/* we don't mark inodes dirty, we just log them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700541static void reiserfs_dirty_inode(struct inode *inode)
542{
543 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700545 int err = 0;
546 if (inode->i_sb->s_flags & MS_RDONLY) {
547 reiserfs_warning(inode->i_sb,
548 "clm-6006: writing inode %lu on readonly FS",
549 inode->i_ino);
550 return;
551 }
552 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700554 /* this is really only used for atime updates, so they don't have
555 ** to be included in O_SYNC or fsync
556 */
557 err = journal_begin(&th, inode->i_sb, 1);
558 if (err) {
559 reiserfs_write_unlock(inode->i_sb);
560 return;
561 }
562 reiserfs_update_sd(&th, inode);
563 journal_end(&th, inode->i_sb, 1);
564 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700567#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700568static void reiserfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700570 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 acl = REISERFS_I(inode)->i_acl_access;
573 if (acl && !IS_ERR(acl))
574 posix_acl_release(acl);
575 REISERFS_I(inode)->i_acl_access = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700577 acl = REISERFS_I(inode)->i_acl_default;
578 if (acl && !IS_ERR(acl))
579 posix_acl_release(acl);
580 REISERFS_I(inode)->i_acl_default = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700582#else
583#define reiserfs_clear_inode NULL
584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700587static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
588 size_t, loff_t);
589static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
590 loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#endif
592
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800593static const struct super_operations reiserfs_sops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700594 .alloc_inode = reiserfs_alloc_inode,
595 .destroy_inode = reiserfs_destroy_inode,
596 .write_inode = reiserfs_write_inode,
597 .dirty_inode = reiserfs_dirty_inode,
598 .delete_inode = reiserfs_delete_inode,
599 .clear_inode = reiserfs_clear_inode,
600 .put_super = reiserfs_put_super,
601 .write_super = reiserfs_write_super,
602 .sync_fs = reiserfs_sync_fs,
603 .write_super_lockfs = reiserfs_write_super_lockfs,
604 .unlockfs = reiserfs_unlockfs,
605 .statfs = reiserfs_statfs,
606 .remount_fs = reiserfs_remount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700608 .quota_read = reiserfs_quota_read,
609 .quota_write = reiserfs_quota_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610#endif
611};
612
613#ifdef CONFIG_QUOTA
614#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
615
616static int reiserfs_dquot_initialize(struct inode *, int);
617static int reiserfs_dquot_drop(struct inode *);
618static int reiserfs_write_dquot(struct dquot *);
619static int reiserfs_acquire_dquot(struct dquot *);
620static int reiserfs_release_dquot(struct dquot *);
621static int reiserfs_mark_dquot_dirty(struct dquot *);
622static int reiserfs_write_info(struct super_block *, int);
623static int reiserfs_quota_on(struct super_block *, int, int, char *);
624
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700625static struct dquot_operations reiserfs_quota_operations = {
626 .initialize = reiserfs_dquot_initialize,
627 .drop = reiserfs_dquot_drop,
628 .alloc_space = dquot_alloc_space,
629 .alloc_inode = dquot_alloc_inode,
630 .free_space = dquot_free_space,
631 .free_inode = dquot_free_inode,
632 .transfer = dquot_transfer,
633 .write_dquot = reiserfs_write_dquot,
634 .acquire_dquot = reiserfs_acquire_dquot,
635 .release_dquot = reiserfs_release_dquot,
636 .mark_dirty = reiserfs_mark_dquot_dirty,
637 .write_info = reiserfs_write_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638};
639
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700640static struct quotactl_ops reiserfs_qctl_operations = {
641 .quota_on = reiserfs_quota_on,
642 .quota_off = vfs_quota_off,
643 .quota_sync = vfs_quota_sync,
644 .get_info = vfs_get_dqinfo,
645 .set_info = vfs_set_dqinfo,
646 .get_dqblk = vfs_get_dqblk,
647 .set_dqblk = vfs_set_dqblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648};
649#endif
650
651static struct export_operations reiserfs_export_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700652 .encode_fh = reiserfs_encode_fh,
653 .decode_fh = reiserfs_decode_fh,
654 .get_parent = reiserfs_get_parent,
655 .get_dentry = reiserfs_get_dentry,
656};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658/* this struct is used in reiserfs_getopt () for containing the value for those
659 mount options that have values rather than being toggles. */
660typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700661 char *value;
662 int setmask; /* bitmask which is to set on mount_options bitmask when this
663 value is found, 0 is no bits are to be changed. */
664 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
665 value is found, 0 is no bits are to be changed. This is
666 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667} arg_desc_t;
668
669/* Set this bit in arg_required to allow empty arguments */
670#define REISERFS_OPT_ALLOWEMPTY 31
671
672/* this struct is used in reiserfs_getopt() for describing the set of reiserfs
673 mount options */
674typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700675 char *option_name;
676 int arg_required; /* 0 if argument is not required, not 0 otherwise */
677 const arg_desc_t *values; /* list of values accepted by an option */
678 int setmask; /* bitmask which is to set on mount_options bitmask when this
679 value is found, 0 is no bits are to be changed. */
680 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
681 value is found, 0 is no bits are to be changed. This is
682 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683} opt_desc_t;
684
685/* possible values for -o data= */
686static const arg_desc_t logging_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700687 {"ordered", 1 << REISERFS_DATA_ORDERED,
688 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
689 {"journal", 1 << REISERFS_DATA_LOG,
690 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
691 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
692 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800693 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694};
695
696/* possible values for -o barrier= */
697static const arg_desc_t barrier_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700698 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
699 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800700 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701};
702
703/* possible values for "-o block-allocator=" and bits which are to be set in
704 s_mount_opt of reiserfs specific part of in-core super block */
705static const arg_desc_t balloc[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700706 {"noborder", 1 << REISERFS_NO_BORDER, 0},
707 {"border", 0, 1 << REISERFS_NO_BORDER},
708 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
709 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
710 {"test4", 1 << REISERFS_TEST4, 0},
711 {"notest4", 0, 1 << REISERFS_TEST4},
712 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713};
714
715static const arg_desc_t tails[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
717 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
718 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
719 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720};
721
722static const arg_desc_t error_actions[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700723 {"panic", 1 << REISERFS_ERROR_PANIC,
724 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
725 {"ro-remount", 1 << REISERFS_ERROR_RO,
726 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700728 {"continue", 1 << REISERFS_ERROR_CONTINUE,
729 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700731 {NULL, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732};
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/* proceed only one option from a list *cur - string containing of mount options
735 opts - array of options which are accepted
736 opt_arg - if option is found and requires an argument and if it is specifed
737 in the input - pointer to the argument is stored here
738 bit_flags - if option requires to set a certain bit - it is set here
739 return -1 if unknown option is found, opt->arg_required otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700740static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
741 char **opt_arg, unsigned long *bit_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 char *p;
744 /* foo=bar,
745 ^ ^ ^
746 | | +-- option_end
747 | +-- arg_start
748 +-- option_start
749 */
750 const opt_desc_t *opt;
751 const arg_desc_t *arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700753 p = *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700755 /* assume argument cannot contain commas */
756 *cur = strchr(p, ',');
757 if (*cur) {
758 *(*cur) = '\0';
759 (*cur)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700762 if (!strncmp(p, "alloc=", 6)) {
763 /* Ugly special case, probably we should redo options parser so that
764 it can understand several arguments for some options, also so that
765 it can fill several bitfields with option values. */
766 if (reiserfs_parse_alloc_options(s, p + 6)) {
767 return -1;
768 } else {
769 return 0;
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700772
773 /* for every option in the list */
774 for (opt = opts; opt->option_name; opt++) {
775 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
776 if (bit_flags) {
777 if (opt->clrmask ==
778 (1 << REISERFS_UNSUPPORTED_OPT))
779 reiserfs_warning(s, "%s not supported.",
780 p);
781 else
782 *bit_flags &= ~opt->clrmask;
783 if (opt->setmask ==
784 (1 << REISERFS_UNSUPPORTED_OPT))
785 reiserfs_warning(s, "%s not supported.",
786 p);
787 else
788 *bit_flags |= opt->setmask;
789 }
790 break;
791 }
792 }
793 if (!opt->option_name) {
794 reiserfs_warning(s, "unknown mount option \"%s\"", p);
795 return -1;
796 }
797
798 p += strlen(opt->option_name);
799 switch (*p) {
800 case '=':
801 if (!opt->arg_required) {
802 reiserfs_warning(s,
803 "the option \"%s\" does not require an argument",
804 opt->option_name);
805 return -1;
806 }
807 break;
808
809 case 0:
810 if (opt->arg_required) {
811 reiserfs_warning(s,
812 "the option \"%s\" requires an argument",
813 opt->option_name);
814 return -1;
815 }
816 break;
817 default:
818 reiserfs_warning(s, "head of option \"%s\" is only correct",
819 opt->option_name);
820 return -1;
821 }
822
823 /* move to the argument, or to next option if argument is not required */
824 p++;
825
826 if (opt->arg_required
827 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
828 && !strlen(p)) {
829 /* this catches "option=," if not allowed */
830 reiserfs_warning(s, "empty argument for \"%s\"",
831 opt->option_name);
832 return -1;
833 }
834
835 if (!opt->values) {
836 /* *=NULLopt_arg contains pointer to argument */
837 *opt_arg = p;
838 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
839 }
840
841 /* values possible for this option are listed in opt->values */
842 for (arg = opt->values; arg->value; arg++) {
843 if (!strcmp(p, arg->value)) {
844 if (bit_flags) {
845 *bit_flags &= ~arg->clrmask;
846 *bit_flags |= arg->setmask;
847 }
848 return opt->arg_required;
849 }
850 }
851
852 reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
853 opt->option_name);
854 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857/* returns 0 if something is wrong in option string, 1 - otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700858static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
859 unsigned long *mount_options,
860 /* after the parsing phase, contains the
861 collection of bitflags defining what
862 mount options were selected. */
863 unsigned long *blocks, /* strtol-ed from NNN of resize=NNN */
864 char **jdev_name,
865 unsigned int *commit_max_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700867 int c;
868 char *arg = NULL;
869 char *pos;
870 opt_desc_t opts[] = {
871 /* Compatibility stuff, so that -o notail for old setups still work */
872 {"tails",.arg_required = 't',.values = tails},
873 {"notail",.clrmask =
874 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
875 {"conv",.setmask = 1 << REISERFS_CONVERT},
876 {"attrs",.setmask = 1 << REISERFS_ATTRS},
877 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700879 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
880 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700882 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
883 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884#endif
885#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700886 {"acl",.setmask = 1 << REISERFS_POSIXACL},
887 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
890 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#endif
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800892 {.option_name = "nolog"},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700893 {"replayonly",.setmask = 1 << REPLAYONLY},
894 {"block-allocator",.arg_required = 'a',.values = balloc},
895 {"data",.arg_required = 'd',.values = logging_mode},
896 {"barrier",.arg_required = 'b',.values = barrier_mode},
897 {"resize",.arg_required = 'r',.values = NULL},
898 {"jdev",.arg_required = 'j',.values = NULL},
899 {"nolargeio",.arg_required = 'w',.values = NULL},
900 {"commit",.arg_required = 'c',.values = NULL},
901 {"usrquota",.setmask = 1 << REISERFS_QUOTA},
902 {"grpquota",.setmask = 1 << REISERFS_QUOTA},
903 {"noquota",.clrmask = 1 << REISERFS_QUOTA},
904 {"errors",.arg_required = 'e',.values = error_actions},
905 {"usrjquota",.arg_required =
906 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
907 {"grpjquota",.arg_required =
908 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
909 {"jqfmt",.arg_required = 'f',.values = NULL},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800910 {.option_name = NULL}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700911 };
912
913 *blocks = 0;
914 if (!options || !*options)
915 /* use default configuration: create tails, journaling on, no
916 conversion to newest format */
917 return 1;
918
919 for (pos = options; pos;) {
920 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
921 if (c == -1)
922 /* wrong option is given */
923 return 0;
924
925 if (c == 'r') {
926 char *p;
927
928 p = NULL;
929 /* "resize=NNN" or "resize=auto" */
930
931 if (!strcmp(arg, "auto")) {
932 /* From JFS code, to auto-get the size. */
933 *blocks =
934 s->s_bdev->bd_inode->i_size >> s->
935 s_blocksize_bits;
936 } else {
937 *blocks = simple_strtoul(arg, &p, 0);
938 if (*p != '\0') {
939 /* NNN does not look like a number */
940 reiserfs_warning(s,
941 "reiserfs_parse_options: bad value %s",
942 arg);
943 return 0;
944 }
945 }
946 }
947
948 if (c == 'c') {
949 char *p = NULL;
950 unsigned long val = simple_strtoul(arg, &p, 0);
951 /* commit=NNN (time in seconds) */
952 if (*p != '\0' || val >= (unsigned int)-1) {
953 reiserfs_warning(s,
954 "reiserfs_parse_options: bad value %s",
955 arg);
956 return 0;
957 }
958 *commit_max_age = (unsigned int)val;
959 }
960
961 if (c == 'w') {
Adrian Bunk36b756f2006-09-27 01:50:50 -0700962 reiserfs_warning(s, "reiserfs: nolargeio option is no longer supported");
963 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700964 }
965
966 if (c == 'j') {
967 if (arg && *arg && jdev_name) {
968 if (*jdev_name) { //Hm, already assigned?
969 reiserfs_warning(s,
970 "reiserfs_parse_options: journal device was already specified to be %s",
971 *jdev_name);
972 return 0;
973 }
974 *jdev_name = arg;
975 }
976 }
977#ifdef CONFIG_QUOTA
978 if (c == 'u' || c == 'g') {
979 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
980
981 if (sb_any_quota_enabled(s)) {
982 reiserfs_warning(s,
983 "reiserfs_parse_options: cannot change journalled quota options when quota turned on.");
984 return 0;
985 }
986 if (*arg) { /* Some filename specified? */
987 if (REISERFS_SB(s)->s_qf_names[qtype]
988 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
989 arg)) {
990 reiserfs_warning(s,
991 "reiserfs_parse_options: %s quota file already specified.",
992 QTYPE2NAME(qtype));
993 return 0;
994 }
995 if (strchr(arg, '/')) {
996 reiserfs_warning(s,
997 "reiserfs_parse_options: quotafile must be on filesystem root.");
998 return 0;
999 }
1000 REISERFS_SB(s)->s_qf_names[qtype] =
1001 kmalloc(strlen(arg) + 1, GFP_KERNEL);
1002 if (!REISERFS_SB(s)->s_qf_names[qtype]) {
1003 reiserfs_warning(s,
1004 "reiserfs_parse_options: not enough memory for storing quotafile name.");
1005 return 0;
1006 }
1007 strcpy(REISERFS_SB(s)->s_qf_names[qtype], arg);
1008 *mount_options |= 1 << REISERFS_QUOTA;
1009 } else {
James Lamanna833d3042005-10-30 15:00:16 -08001010 kfree(REISERFS_SB(s)->s_qf_names[qtype]);
1011 REISERFS_SB(s)->s_qf_names[qtype] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001012 }
1013 }
1014 if (c == 'f') {
1015 if (!strcmp(arg, "vfsold"))
1016 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_OLD;
1017 else if (!strcmp(arg, "vfsv0"))
1018 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_V0;
1019 else {
1020 reiserfs_warning(s,
1021 "reiserfs_parse_options: unknown quota format specified.");
1022 return 0;
1023 }
1024 }
1025#else
1026 if (c == 'u' || c == 'g' || c == 'f') {
1027 reiserfs_warning(s,
1028 "reiserfs_parse_options: journalled quota options not supported.");
1029 return 0;
1030 }
1031#endif
1032 }
1033
1034#ifdef CONFIG_QUOTA
1035 if (!REISERFS_SB(s)->s_jquota_fmt
1036 && (REISERFS_SB(s)->s_qf_names[USRQUOTA]
1037 || REISERFS_SB(s)->s_qf_names[GRPQUOTA])) {
1038 reiserfs_warning(s,
1039 "reiserfs_parse_options: journalled quota format not specified.");
1040 return 0;
1041 }
1042 /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
1043 if (!(*mount_options & (1 << REISERFS_QUOTA))
1044 && sb_any_quota_enabled(s)) {
1045 reiserfs_warning(s,
1046 "reiserfs_parse_options: quota options must be present when quota is turned on.");
1047 return 0;
1048 }
1049#endif
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001054static void switch_data_mode(struct super_block *s, unsigned long mode)
1055{
1056 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1057 (1 << REISERFS_DATA_ORDERED) |
1058 (1 << REISERFS_DATA_WRITEBACK));
1059 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1063{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001064 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1065 if (!reiserfs_data_log(s)) {
1066 switch_data_mode(s, REISERFS_DATA_LOG);
1067 reiserfs_info(s, "switching to journaled data mode\n");
1068 }
1069 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1070 if (!reiserfs_data_ordered(s)) {
1071 switch_data_mode(s, REISERFS_DATA_ORDERED);
1072 reiserfs_info(s, "switching to ordered data mode\n");
1073 }
1074 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1075 if (!reiserfs_data_writeback(s)) {
1076 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1077 reiserfs_info(s, "switching to writeback data mode\n");
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001082static void handle_barrier_mode(struct super_block *s, unsigned long bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001084 int flush = (1 << REISERFS_BARRIER_FLUSH);
1085 int none = (1 << REISERFS_BARRIER_NONE);
1086 int all_barrier = flush | none;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001088 if (bits & all_barrier) {
1089 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1090 if (bits & flush) {
1091 REISERFS_SB(s)->s_mount_opt |= flush;
1092 printk("reiserfs: enabling write barrier flush mode\n");
1093 } else if (bits & none) {
1094 REISERFS_SB(s)->s_mount_opt |= none;
1095 printk("reiserfs: write barriers turned off\n");
1096 }
1097 }
1098}
1099
1100static void handle_attrs(struct super_block *s)
1101{
1102 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1103
1104 if (reiserfs_attrs(s)) {
1105 if (old_format_only(s)) {
1106 reiserfs_warning(s,
1107 "reiserfs: cannot support attributes on 3.5.x disk format");
1108 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return;
1110 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001111 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1112 reiserfs_warning(s,
1113 "reiserfs: cannot support attributes until flag is set in super-block");
1114 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116 }
1117}
1118
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001119static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001121 struct reiserfs_super_block *rs;
1122 struct reiserfs_transaction_handle th;
1123 unsigned long blocks;
1124 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1125 unsigned long safe_mask = 0;
1126 unsigned int commit_max_age = (unsigned int)-1;
1127 struct reiserfs_journal *journal = SB_JOURNAL(s);
1128 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001130 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131#endif
1132
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001133 rs = SB_DISK_SUPER_BLOCK(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001135 if (!reiserfs_parse_options
1136 (s, arg, &mount_options, &blocks, NULL, &commit_max_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137#ifdef CONFIG_QUOTA
James Lamanna833d3042005-10-30 15:00:16 -08001138 for (i = 0; i < MAXQUOTAS; i++) {
1139 kfree(REISERFS_SB(s)->s_qf_names[i]);
1140 REISERFS_SB(s)->s_qf_names[i] = NULL;
1141 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001142#endif
1143 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001146 handle_attrs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001148 /* Add options that are safe here */
1149 safe_mask |= 1 << REISERFS_SMALLTAIL;
1150 safe_mask |= 1 << REISERFS_LARGETAIL;
1151 safe_mask |= 1 << REISERFS_NO_BORDER;
1152 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1153 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1154 safe_mask |= 1 << REISERFS_TEST4;
1155 safe_mask |= 1 << REISERFS_ATTRS;
1156 safe_mask |= 1 << REISERFS_XATTRS_USER;
1157 safe_mask |= 1 << REISERFS_POSIXACL;
1158 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1159 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1160 safe_mask |= 1 << REISERFS_ERROR_RO;
1161 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1162 safe_mask |= 1 << REISERFS_ERROR_PANIC;
1163 safe_mask |= 1 << REISERFS_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001165 /* Update the bitmask, taking care to keep
1166 * the bits we're not allowed to change here */
1167 REISERFS_SB(s)->s_mount_opt =
1168 (REISERFS_SB(s)->
1169 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001171 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1172 journal->j_max_commit_age = commit_max_age;
1173 journal->j_max_trans_age = commit_max_age;
1174 } else if (commit_max_age == 0) {
1175 /* 0 means restore defaults. */
1176 journal->j_max_commit_age = journal->j_default_max_commit_age;
1177 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001180 if (blocks) {
1181 int rc = reiserfs_resize(s, blocks);
1182 if (rc != 0)
1183 return rc;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001186 if (*mount_flags & MS_RDONLY) {
1187 reiserfs_xattr_init(s, *mount_flags);
1188 /* remount read-only */
1189 if (s->s_flags & MS_RDONLY)
1190 /* it is read-only already */
1191 return 0;
1192 /* try to remount file system with read-only permissions */
1193 if (sb_umount_state(rs) == REISERFS_VALID_FS
1194 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1195 return 0;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001198 err = journal_begin(&th, s, 10);
1199 if (err)
1200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001202 /* Mounting a rw partition read-only. */
1203 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1204 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1205 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1206 } else {
1207 /* remount read-write */
1208 if (!(s->s_flags & MS_RDONLY)) {
1209 reiserfs_xattr_init(s, *mount_flags);
1210 return 0; /* We are read-write already */
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001213 if (reiserfs_is_journal_aborted(journal))
1214 return journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001216 handle_data_mode(s, mount_options);
1217 handle_barrier_mode(s, mount_options);
1218 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1219 s->s_flags &= ~MS_RDONLY; /* now it is safe to call journal_begin */
1220 err = journal_begin(&th, s, 10);
1221 if (err)
1222 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001224 /* Mount a partition which is read-only, read-write */
1225 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1226 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1227 s->s_flags &= ~MS_RDONLY;
1228 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1229 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1230 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1231 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1232 }
1233 /* this will force a full flush of all journal lists */
1234 SB_JOURNAL(s)->j_must_wait = 1;
1235 err = journal_end(&th, s, 10);
1236 if (err)
1237 return err;
1238 s->s_dirt = 0;
1239
1240 if (!(*mount_flags & MS_RDONLY)) {
1241 finish_unfinished(s);
1242 reiserfs_xattr_init(s, *mount_flags);
1243 }
1244
1245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001248static int read_super_block(struct super_block *s, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001250 struct buffer_head *bh;
1251 struct reiserfs_super_block *rs;
1252 int fs_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001254 bh = sb_bread(s, offset / s->s_blocksize);
1255 if (!bh) {
1256 reiserfs_warning(s, "sh-2006: read_super_block: "
1257 "bread failed (dev %s, block %lu, size %lu)",
1258 reiserfs_bdevname(s), offset / s->s_blocksize,
1259 s->s_blocksize);
1260 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001263 rs = (struct reiserfs_super_block *)bh->b_data;
1264 if (!is_any_reiserfs_magic_string(rs)) {
1265 brelse(bh);
1266 return 1;
1267 }
1268 //
1269 // ok, reiserfs signature (old or new) found in at the given offset
1270 //
1271 fs_blocksize = sb_blocksize(rs);
1272 brelse(bh);
1273 sb_set_blocksize(s, fs_blocksize);
1274
1275 bh = sb_bread(s, offset / s->s_blocksize);
1276 if (!bh) {
1277 reiserfs_warning(s, "sh-2007: read_super_block: "
1278 "bread failed (dev %s, block %lu, size %lu)\n",
1279 reiserfs_bdevname(s), offset / s->s_blocksize,
1280 s->s_blocksize);
1281 return 1;
1282 }
1283
1284 rs = (struct reiserfs_super_block *)bh->b_data;
1285 if (sb_blocksize(rs) != s->s_blocksize) {
1286 reiserfs_warning(s, "sh-2011: read_super_block: "
1287 "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1288 reiserfs_bdevname(s),
1289 (unsigned long long)bh->b_blocknr,
1290 s->s_blocksize);
1291 brelse(bh);
1292 return 1;
1293 }
1294
1295 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1296 brelse(bh);
1297 reiserfs_warning(s,
1298 "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1299 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1300 "get newer reiserfsprogs package");
1301 return 1;
1302 }
1303
1304 SB_BUFFER_WITH_SB(s) = bh;
1305 SB_DISK_SUPER_BLOCK(s) = rs;
1306
1307 if (is_reiserfs_jr(rs)) {
1308 /* magic is of non-standard journal filesystem, look at s_version to
1309 find which format is in use */
1310 if (sb_version(rs) == REISERFS_VERSION_2)
1311 reiserfs_warning(s,
1312 "read_super_block: found reiserfs format \"3.6\""
1313 " with non-standard journal");
1314 else if (sb_version(rs) == REISERFS_VERSION_1)
1315 reiserfs_warning(s,
1316 "read_super_block: found reiserfs format \"3.5\""
1317 " with non-standard journal");
1318 else {
1319 reiserfs_warning(s,
1320 "sh-2012: read_super_block: found unknown "
1321 "format \"%u\" of reiserfs with non-standard magic",
1322 sb_version(rs));
1323 return 1;
1324 }
1325 } else
1326 /* s_version of standard format may contain incorrect information,
1327 so we just look at the magic string */
1328 reiserfs_info(s,
1329 "found reiserfs format \"%s\" with standard journal\n",
1330 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1331
1332 s->s_op = &reiserfs_sops;
1333 s->s_export_op = &reiserfs_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001335 s->s_qcop = &reiserfs_qctl_operations;
1336 s->dq_op = &reiserfs_quota_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337#endif
1338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001339 /* new format is limited by the 32 bit wide i_blocks field, want to
1340 ** be one full block below that.
1341 */
1342 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/* after journal replay, reread all bitmap and super blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001347static int reread_meta_blocks(struct super_block *s)
1348{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001349 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
1350 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1351 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1352 reiserfs_warning(s,
1353 "reread_meta_blocks, error reading the super");
1354 return 1;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/////////////////////////////////////////////////////
1361// hash detection stuff
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363// if root directory is empty - we set default - Yura's - hash and
1364// warn about it
1365// FIXME: we look for only one name in a directory. If tea and yura
1366// bith have the same value - we ask user to send report to the
1367// mailing list
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001368static __u32 find_hash_out(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001370 int retval;
1371 struct inode *inode;
1372 struct cpu_key key;
1373 INITIALIZE_PATH(path);
1374 struct reiserfs_dir_entry de;
1375 __u32 hash = DEFAULT_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001377 inode = s->s_root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001379 do { // Some serious "goto"-hater was there ;)
1380 u32 teahash, r5hash, yurahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001382 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1383 retval = search_by_entry_key(s, &key, &path, &de);
1384 if (retval == IO_ERROR) {
1385 pathrelse(&path);
1386 return UNSET_HASH;
1387 }
1388 if (retval == NAME_NOT_FOUND)
1389 de.de_entry_num--;
1390 set_de_name_and_namelen(&de);
1391 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1392 /* allow override in this case */
1393 if (reiserfs_rupasov_hash(s)) {
1394 hash = YURA_HASH;
1395 }
1396 reiserfs_warning(s, "FS seems to be empty, autodetect "
1397 "is using the default hash");
1398 break;
1399 }
1400 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1401 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1402 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1403 if (((teahash == r5hash)
1404 &&
1405 (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1406 == r5hash)) || ((teahash == yurahash)
1407 && (yurahash ==
1408 GET_HASH_VALUE(deh_offset
1409 (&
1410 (de.
1411 de_deh[de.
1412 de_entry_num])))))
1413 || ((r5hash == yurahash)
1414 && (yurahash ==
1415 GET_HASH_VALUE(deh_offset
1416 (&(de.de_deh[de.de_entry_num])))))) {
1417 reiserfs_warning(s,
1418 "Unable to automatically detect hash function. "
1419 "Please mount with -o hash={tea,rupasov,r5}",
1420 reiserfs_bdevname(s));
1421 hash = UNSET_HASH;
1422 break;
1423 }
1424 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1425 yurahash)
1426 hash = YURA_HASH;
1427 else if (GET_HASH_VALUE
1428 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1429 hash = TEA_HASH;
1430 else if (GET_HASH_VALUE
1431 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1432 hash = R5_HASH;
1433 else {
1434 reiserfs_warning(s, "Unrecognised hash function");
1435 hash = UNSET_HASH;
1436 }
1437 } while (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001439 pathrelse(&path);
1440 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443// finds out which hash names are sorted with
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001444static int what_hash(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001446 __u32 code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001448 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001450 /* reiserfs_hash_detect() == true if any of the hash mount options
1451 ** were used. We must check them to make sure the user isn't
1452 ** using a bad hash value
1453 */
1454 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1455 code = find_hash_out(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001457 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1458 /* detection has found the hash, and we must check against the
1459 ** mount options
1460 */
1461 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1462 reiserfs_warning(s, "Error, %s hash detected, "
1463 "unable to force rupasov hash",
1464 reiserfs_hashname(code));
1465 code = UNSET_HASH;
1466 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1467 reiserfs_warning(s, "Error, %s hash detected, "
1468 "unable to force tea hash",
1469 reiserfs_hashname(code));
1470 code = UNSET_HASH;
1471 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1472 reiserfs_warning(s, "Error, %s hash detected, "
1473 "unable to force r5 hash",
1474 reiserfs_hashname(code));
1475 code = UNSET_HASH;
1476 }
1477 } else {
1478 /* find_hash_out was not called or could not determine the hash */
1479 if (reiserfs_rupasov_hash(s)) {
1480 code = YURA_HASH;
1481 } else if (reiserfs_tea_hash(s)) {
1482 code = TEA_HASH;
1483 } else if (reiserfs_r5_hash(s)) {
1484 code = R5_HASH;
1485 }
1486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001488 /* if we are mounted RW, and we have a new valid hash code, update
1489 ** the super
1490 */
1491 if (code != UNSET_HASH &&
1492 !(s->s_flags & MS_RDONLY) &&
1493 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1494 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1495 }
1496 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499// return pointer to appropriate function
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001500static hashf_t hash_function(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001502 switch (what_hash(s)) {
1503 case TEA_HASH:
1504 reiserfs_info(s, "Using tea hash to sort names\n");
1505 return keyed_hash;
1506 case YURA_HASH:
1507 reiserfs_info(s, "Using rupasov hash to sort names\n");
1508 return yura_hash;
1509 case R5_HASH:
1510 reiserfs_info(s, "Using r5 hash to sort names\n");
1511 return r5_hash;
1512 }
1513 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516// this is used to set up correct value for old partitions
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001517static int function2code(hashf_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001519 if (func == keyed_hash)
1520 return TEA_HASH;
1521 if (func == yura_hash)
1522 return YURA_HASH;
1523 if (func == r5_hash)
1524 return R5_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001526 BUG(); // should never happen
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531#define SWARN(silent, s, ...) \
1532 if (!(silent)) \
1533 reiserfs_warning (s, __VA_ARGS__)
1534
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001535static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001537 struct inode *root_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001538 struct reiserfs_transaction_handle th;
1539 int old_format = 0;
1540 unsigned long blocks;
1541 unsigned int commit_max_age = 0;
1542 int jinit_done = 0;
1543 struct reiserfs_iget_args args;
1544 struct reiserfs_super_block *rs;
1545 char *jdev_name;
1546 struct reiserfs_sb_info *sbi;
1547 int errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Yan Burman01afb212006-12-06 20:39:01 -08001549 sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001550 if (!sbi) {
1551 errval = -ENOMEM;
1552 goto error;
1553 }
1554 s->s_fs_info = sbi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001555 /* Set default values for options: non-aggressive tails, RO on errors */
1556 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1557 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1558 /* no preallocation minimum, be smart in
1559 reiserfs_file_write instead */
1560 REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1561 /* Preallocate by 16 blocks (17-1) at once */
1562 REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001563#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001564 /* Initialize the rwsem for xattr dir */
1565 init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001566#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001567 /* setup default block allocator options */
1568 reiserfs_init_alloc_options(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001570 jdev_name = NULL;
1571 if (reiserfs_parse_options
1572 (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
1573 &commit_max_age) == 0) {
1574 goto error;
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001577 if (blocks) {
1578 SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1579 "for remount only");
1580 goto error;
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001583 /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1584 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1585 old_format = 1;
1586 /* try new format (64-th 1k block), which can contain reiserfs super block */
1587 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1588 SWARN(silent, s,
1589 "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
1590 reiserfs_bdevname(s));
1591 goto error;
1592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001594 rs = SB_DISK_SUPER_BLOCK(s);
1595 /* Let's do basic sanity check to verify that underlying device is not
1596 smaller than the filesystem. If the check fails then abort and scream,
1597 because bad stuff will happen otherwise. */
1598 if (s->s_bdev && s->s_bdev->bd_inode
1599 && i_size_read(s->s_bdev->bd_inode) <
1600 sb_block_count(rs) * sb_blocksize(rs)) {
1601 SWARN(silent, s,
1602 "Filesystem on %s cannot be mounted because it is bigger than the device",
1603 reiserfs_bdevname(s));
1604 SWARN(silent, s,
1605 "You may need to run fsck or increase size of your LVM partition");
1606 SWARN(silent, s,
1607 "Or may be you forgot to reboot after fdisk when it told you to");
1608 goto error;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001611 sbi->s_mount_state = SB_REISERFS_STATE(s);
1612 sbi->s_mount_state = REISERFS_VALID_FS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Jeff Mahoney6f010462006-09-30 23:28:43 -07001614 if ((errval = reiserfs_init_bitmap_cache(s))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001615 SWARN(silent, s,
1616 "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1617 goto error;
1618 }
Jeff Mahoneyd2c89a42006-11-02 22:07:20 -08001619 errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001621 SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
1622 SWARN(silent, s, "- it is slow mode for debugging.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623#endif
1624
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001625 /* make data=ordered the default */
1626 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1627 !reiserfs_data_writeback(s)) {
1628 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001631 if (reiserfs_data_log(s)) {
1632 reiserfs_info(s, "using journaled data mode\n");
1633 } else if (reiserfs_data_ordered(s)) {
1634 reiserfs_info(s, "using ordered data mode\n");
1635 } else {
1636 reiserfs_info(s, "using writeback data mode\n");
1637 }
1638 if (reiserfs_barrier_flush(s)) {
1639 printk("reiserfs: using flush barriers\n");
1640 }
1641 // set_device_ro(s->s_dev, 1) ;
1642 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1643 SWARN(silent, s,
1644 "sh-2022: reiserfs_fill_super: unable to initialize journal space");
1645 goto error;
1646 } else {
1647 jinit_done = 1; /* once this is set, journal_release must be called
1648 ** if we error out of the mount
1649 */
1650 }
1651 if (reread_meta_blocks(s)) {
1652 SWARN(silent, s,
1653 "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
1654 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001657 if (replay_only(s))
1658 goto error;
1659
1660 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1661 SWARN(silent, s,
1662 "clm-7000: Detected readonly device, marking FS readonly");
1663 s->s_flags |= MS_RDONLY;
1664 }
1665 args.objectid = REISERFS_ROOT_OBJECTID;
1666 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
1667 root_inode =
1668 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
1669 reiserfs_init_locked_inode, (void *)(&args));
1670 if (!root_inode) {
1671 SWARN(silent, s,
1672 "jmacd-10: reiserfs_fill_super: get root inode failed");
1673 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001676 if (root_inode->i_state & I_NEW) {
1677 reiserfs_read_locked_inode(root_inode, &args);
1678 unlock_new_inode(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001681 s->s_root = d_alloc_root(root_inode);
1682 if (!s->s_root) {
1683 iput(root_inode);
1684 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001686 // define and initialize hash function
1687 sbi->s_hash_function = hash_function(s);
1688 if (sbi->s_hash_function == NULL) {
1689 dput(s->s_root);
1690 s->s_root = NULL;
1691 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001693
1694 if (is_reiserfs_3_5(rs)
1695 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
1696 set_bit(REISERFS_3_5, &(sbi->s_properties));
Jeff Mahoneye1fabd32006-09-30 23:28:40 -07001697 else if (old_format)
1698 set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001699 else
1700 set_bit(REISERFS_3_6, &(sbi->s_properties));
1701
1702 if (!(s->s_flags & MS_RDONLY)) {
1703
1704 errval = journal_begin(&th, s, 1);
1705 if (errval) {
1706 dput(s->s_root);
1707 s->s_root = NULL;
1708 goto error;
1709 }
1710 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1711
1712 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1713 set_sb_fs_state(rs, 0);
1714
1715 if (old_format_only(s)) {
1716 /* filesystem of format 3.5 either with standard or non-standard
1717 journal */
1718 if (convert_reiserfs(s)) {
1719 /* and -o conv is given */
1720 if (!silent)
1721 reiserfs_info(s,
1722 "converting 3.5 filesystem to the 3.6 format");
1723
1724 if (is_reiserfs_3_5(rs))
1725 /* put magic string of 3.6 format. 2.2 will not be able to
1726 mount this filesystem anymore */
1727 memcpy(rs->s_v1.s_magic,
1728 reiserfs_3_6_magic_string,
1729 sizeof
1730 (reiserfs_3_6_magic_string));
1731
1732 set_sb_version(rs, REISERFS_VERSION_2);
1733 reiserfs_convert_objectid_map_v1(s);
1734 set_bit(REISERFS_3_6, &(sbi->s_properties));
1735 clear_bit(REISERFS_3_5, &(sbi->s_properties));
1736 } else if (!silent) {
1737 reiserfs_info(s, "using 3.5.x disk format\n");
1738 }
1739 }
1740
1741 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1742 errval = journal_end(&th, s, 1);
1743 if (errval) {
1744 dput(s->s_root);
1745 s->s_root = NULL;
1746 goto error;
1747 }
1748
1749 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1750 dput(s->s_root);
1751 s->s_root = NULL;
1752 goto error;
1753 }
1754
1755 /* look for files which were to be removed in previous session */
1756 finish_unfinished(s);
1757 } else {
1758 if (old_format_only(s) && !silent) {
1759 reiserfs_info(s, "using 3.5.x disk format\n");
1760 }
1761
1762 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1763 dput(s->s_root);
1764 s->s_root = NULL;
1765 goto error;
1766 }
1767 }
1768 // mark hash in super block: it could be unset. overwrite should be ok
1769 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
1770
1771 handle_attrs(s);
1772
1773 reiserfs_proc_info_init(s);
1774
1775 init_waitqueue_head(&(sbi->s_wait));
1776 spin_lock_init(&sbi->bitmap_lock);
1777
1778 return (0);
1779
1780 error:
1781 if (jinit_done) { /* kill the commit thread, free journal ram */
1782 journal_release_error(NULL, s);
1783 }
Jeff Mahoney50652272006-09-30 23:28:44 -07001784
1785 reiserfs_free_bitmap_cache(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001786 if (SB_BUFFER_WITH_SB(s))
1787 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788#ifdef CONFIG_QUOTA
Jeff Mahoney50652272006-09-30 23:28:44 -07001789 {
1790 int j;
1791 for (j = 0; j < MAXQUOTAS; j++) {
1792 kfree(sbi->s_qf_names[j]);
1793 sbi->s_qf_names[j] = NULL;
1794 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796#endif
James Lamanna833d3042005-10-30 15:00:16 -08001797 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001799 s->s_fs_info = NULL;
1800 return errval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
David Howells726c3342006-06-23 02:02:58 -07001803static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
David Howells726c3342006-06-23 02:02:58 -07001805 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001806
1807 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
1808 buf->f_bfree = sb_free_blocks(rs);
1809 buf->f_bavail = buf->f_bfree;
1810 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
David Howells726c3342006-06-23 02:02:58 -07001811 buf->f_bsize = dentry->d_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001812 /* changed to accommodate gcc folks. */
1813 buf->f_type = REISERFS_SUPER_MAGIC;
1814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
1817#ifdef CONFIG_QUOTA
1818static int reiserfs_dquot_initialize(struct inode *inode, int type)
1819{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001820 struct reiserfs_transaction_handle th;
1821 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001823 /* We may create quota structure so we need to reserve enough blocks */
1824 reiserfs_write_lock(inode->i_sb);
1825 ret =
1826 journal_begin(&th, inode->i_sb,
1827 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1828 if (ret)
1829 goto out;
1830 ret = dquot_initialize(inode, type);
1831 err =
1832 journal_end(&th, inode->i_sb,
1833 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1834 if (!ret && err)
1835 ret = err;
1836 out:
1837 reiserfs_write_unlock(inode->i_sb);
1838 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
1841static int reiserfs_dquot_drop(struct inode *inode)
1842{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001843 struct reiserfs_transaction_handle th;
1844 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001846 /* We may delete quota structure so we need to reserve enough blocks */
1847 reiserfs_write_lock(inode->i_sb);
1848 ret =
1849 journal_begin(&th, inode->i_sb,
1850 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1851 if (ret)
1852 goto out;
1853 ret = dquot_drop(inode);
1854 err =
1855 journal_end(&th, inode->i_sb,
1856 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1857 if (!ret && err)
1858 ret = err;
1859 out:
1860 reiserfs_write_unlock(inode->i_sb);
1861 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864static int reiserfs_write_dquot(struct dquot *dquot)
1865{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001866 struct reiserfs_transaction_handle th;
1867 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001869 reiserfs_write_lock(dquot->dq_sb);
1870 ret =
1871 journal_begin(&th, dquot->dq_sb,
1872 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1873 if (ret)
1874 goto out;
1875 ret = dquot_commit(dquot);
1876 err =
1877 journal_end(&th, dquot->dq_sb,
1878 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1879 if (!ret && err)
1880 ret = err;
1881 out:
1882 reiserfs_write_unlock(dquot->dq_sb);
1883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
1886static int reiserfs_acquire_dquot(struct dquot *dquot)
1887{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001888 struct reiserfs_transaction_handle th;
1889 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001891 reiserfs_write_lock(dquot->dq_sb);
1892 ret =
1893 journal_begin(&th, dquot->dq_sb,
1894 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1895 if (ret)
1896 goto out;
1897 ret = dquot_acquire(dquot);
1898 err =
1899 journal_end(&th, dquot->dq_sb,
1900 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1901 if (!ret && err)
1902 ret = err;
1903 out:
1904 reiserfs_write_unlock(dquot->dq_sb);
1905 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
1908static int reiserfs_release_dquot(struct dquot *dquot)
1909{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001910 struct reiserfs_transaction_handle th;
1911 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001913 reiserfs_write_lock(dquot->dq_sb);
1914 ret =
1915 journal_begin(&th, dquot->dq_sb,
1916 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1917 if (ret)
1918 goto out;
1919 ret = dquot_release(dquot);
1920 err =
1921 journal_end(&th, dquot->dq_sb,
1922 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1923 if (!ret && err)
1924 ret = err;
1925 out:
1926 reiserfs_write_unlock(dquot->dq_sb);
1927 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
1930static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
1931{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001932 /* Are we journalling quotas? */
1933 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
1934 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
1935 dquot_mark_dquot_dirty(dquot);
1936 return reiserfs_write_dquot(dquot);
1937 } else
1938 return dquot_mark_dquot_dirty(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941static int reiserfs_write_info(struct super_block *sb, int type)
1942{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001943 struct reiserfs_transaction_handle th;
1944 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001946 /* Data block + inode block */
1947 reiserfs_write_lock(sb);
1948 ret = journal_begin(&th, sb, 2);
1949 if (ret)
1950 goto out;
1951 ret = dquot_commit_info(sb, type);
1952 err = journal_end(&th, sb, 2);
1953 if (!ret && err)
1954 ret = err;
1955 out:
1956 reiserfs_write_unlock(sb);
1957 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960/*
Christoph Hellwig84de8562005-06-23 00:09:16 -07001961 * Turn on quotas during mount time - we need to find the quota file and such...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
1963static int reiserfs_quota_on_mount(struct super_block *sb, int type)
1964{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001965 return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001966 REISERFS_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969/*
1970 * Standard function to be called on quota_on
1971 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001972static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
1973 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001975 int err;
1976 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001978 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
1979 return -EINVAL;
1980 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
1981 if (err)
1982 return err;
1983 /* Quotafile not on the same filesystem? */
1984 if (nd.mnt->mnt_sb != sb) {
1985 path_release(&nd);
1986 return -EXDEV;
1987 }
1988 /* We must not pack tails for quota files on reiserfs for quota IO to work */
1989 if (!REISERFS_I(nd.dentry->d_inode)->i_flags & i_nopack_mask) {
1990 reiserfs_warning(sb,
1991 "reiserfs: Quota file must have tail packing disabled.");
1992 path_release(&nd);
1993 return -EINVAL;
1994 }
1995 /* Not journalling quota? No more tests needed... */
1996 if (!REISERFS_SB(sb)->s_qf_names[USRQUOTA] &&
1997 !REISERFS_SB(sb)->s_qf_names[GRPQUOTA]) {
1998 path_release(&nd);
1999 return vfs_quota_on(sb, type, format_id, path);
2000 }
2001 /* Quotafile not of fs root? */
2002 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2003 reiserfs_warning(sb,
2004 "reiserfs: Quota file not on filesystem root. "
2005 "Journalled quota will not work.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 path_release(&nd);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002007 return vfs_quota_on(sb, type, format_id, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
2010/* Read data from quotafile - avoid pagecache and such because we cannot afford
2011 * acquiring the locks... As quota files are never truncated and quota code
2012 * itself serializes the operations (and noone else should touch the files)
2013 * we don't have to be afraid of races */
2014static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2015 size_t len, loff_t off)
2016{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002017 struct inode *inode = sb_dqopt(sb)->files[type];
2018 unsigned long blk = off >> sb->s_blocksize_bits;
2019 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2020 size_t toread;
2021 struct buffer_head tmp_bh, *bh;
2022 loff_t i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002024 if (off > i_size)
2025 return 0;
2026 if (off + len > i_size)
2027 len = i_size - off;
2028 toread = len;
2029 while (toread > 0) {
2030 tocopy =
2031 sb->s_blocksize - offset <
2032 toread ? sb->s_blocksize - offset : toread;
2033 tmp_bh.b_state = 0;
2034 /* Quota files are without tails so we can safely use this function */
2035 reiserfs_write_lock(sb);
2036 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2037 reiserfs_write_unlock(sb);
2038 if (err)
2039 return err;
2040 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2041 memset(data, 0, tocopy);
2042 else {
2043 bh = sb_bread(sb, tmp_bh.b_blocknr);
2044 if (!bh)
2045 return -EIO;
2046 memcpy(data, bh->b_data + offset, tocopy);
2047 brelse(bh);
2048 }
2049 offset = 0;
2050 toread -= tocopy;
2051 data += tocopy;
2052 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002054 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057/* Write to quotafile (we know the transaction is already started and has
2058 * enough credits) */
2059static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2060 const char *data, size_t len, loff_t off)
2061{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002062 struct inode *inode = sb_dqopt(sb)->files[type];
2063 unsigned long blk = off >> sb->s_blocksize_bits;
2064 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2065 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2066 size_t towrite = len;
2067 struct buffer_head tmp_bh, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Arjan van de Ven5c81a412006-07-03 00:25:20 -07002069 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002070 while (towrite > 0) {
2071 tocopy = sb->s_blocksize - offset < towrite ?
2072 sb->s_blocksize - offset : towrite;
2073 tmp_bh.b_state = 0;
2074 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2075 if (err)
2076 goto out;
2077 if (offset || tocopy != sb->s_blocksize)
2078 bh = sb_bread(sb, tmp_bh.b_blocknr);
2079 else
2080 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2081 if (!bh) {
2082 err = -EIO;
2083 goto out;
2084 }
2085 lock_buffer(bh);
2086 memcpy(bh->b_data + offset, data, tocopy);
2087 flush_dcache_page(bh->b_page);
2088 set_buffer_uptodate(bh);
2089 unlock_buffer(bh);
2090 reiserfs_prepare_for_journal(sb, bh, 1);
2091 journal_mark_dirty(current->journal_info, sb, bh);
2092 if (!journal_quota)
2093 reiserfs_add_ordered_list(inode, bh);
2094 brelse(bh);
2095 offset = 0;
2096 towrite -= tocopy;
2097 data += tocopy;
2098 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002100 out:
2101 if (len == towrite)
2102 return err;
2103 if (inode->i_size < off + len - towrite)
2104 i_size_write(inode, off + len - towrite);
2105 inode->i_version++;
2106 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2107 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002108 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002109 return len - towrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112#endif
2113
David Howells454e2392006-06-23 02:02:57 -07002114static int get_super_block(struct file_system_type *fs_type,
2115 int flags, const char *dev_name,
2116 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
David Howells454e2392006-06-23 02:02:57 -07002118 return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
2119 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002122static int __init init_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 int ret;
2125
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002126 if ((ret = init_inodecache())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return ret;
2128 }
2129
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002130 if ((ret = reiserfs_xattr_register_handlers()))
2131 goto failed_reiserfs_xattr_register_handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002133 reiserfs_proc_info_global_init();
2134 reiserfs_proc_register_global("version",
2135 reiserfs_global_version_in_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002137 ret = register_filesystem(&reiserfs_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 if (ret == 0) {
2140 return 0;
2141 }
2142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002143 reiserfs_xattr_unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002145 failed_reiserfs_xattr_register_handlers:
2146 reiserfs_proc_unregister_global("version");
2147 reiserfs_proc_info_global_done();
2148 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 return ret;
2151}
2152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002153static void __exit exit_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002155 reiserfs_xattr_unregister_handlers();
2156 reiserfs_proc_unregister_global("version");
2157 reiserfs_proc_info_global_done();
2158 unregister_filesystem(&reiserfs_fs_type);
2159 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
2162struct file_system_type reiserfs_fs_type = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002163 .owner = THIS_MODULE,
2164 .name = "reiserfs",
2165 .get_sb = get_super_block,
David Howellsedc666e2006-10-11 01:22:14 -07002166 .kill_sb = reiserfs_kill_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002167 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168};
2169
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002170MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2171MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2172MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002174module_init(init_reiserfs_fs);
2175module_exit(exit_reiserfs_fs);