blob: c7762140c4258a3ef8c867236a25da67ad38e51f [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 Lameter50953fe2007-05-06 14:50:16 -0700514 if (flags & SLAB_CTOR_CONSTRUCTOR) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700515 INIT_LIST_HEAD(&ei->i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 inode_init_once(&ei->vfs_inode);
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700517#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ei->i_acl_access = NULL;
519 ei->i_acl_default = NULL;
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524static int init_inodecache(void)
525{
526 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700527 sizeof(struct
528 reiserfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800529 0, (SLAB_RECLAIM_ACCOUNT|
530 SLAB_MEM_SPREAD),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700531 init_once, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (reiserfs_inode_cachep == NULL)
533 return -ENOMEM;
534 return 0;
535}
536
537static void destroy_inodecache(void)
538{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700539 kmem_cache_destroy(reiserfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542/* we don't mark inodes dirty, we just log them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700543static void reiserfs_dirty_inode(struct inode *inode)
544{
545 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700547 int err = 0;
548 if (inode->i_sb->s_flags & MS_RDONLY) {
549 reiserfs_warning(inode->i_sb,
550 "clm-6006: writing inode %lu on readonly FS",
551 inode->i_ino);
552 return;
553 }
554 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700556 /* this is really only used for atime updates, so they don't have
557 ** to be included in O_SYNC or fsync
558 */
559 err = journal_begin(&th, inode->i_sb, 1);
560 if (err) {
561 reiserfs_write_unlock(inode->i_sb);
562 return;
563 }
564 reiserfs_update_sd(&th, inode);
565 journal_end(&th, inode->i_sb, 1);
566 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700569#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700570static void reiserfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700574 acl = REISERFS_I(inode)->i_acl_access;
575 if (acl && !IS_ERR(acl))
576 posix_acl_release(acl);
577 REISERFS_I(inode)->i_acl_access = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700579 acl = REISERFS_I(inode)->i_acl_default;
580 if (acl && !IS_ERR(acl))
581 posix_acl_release(acl);
582 REISERFS_I(inode)->i_acl_default = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700584#else
585#define reiserfs_clear_inode NULL
586#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700589static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
590 size_t, loff_t);
591static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
592 loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#endif
594
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800595static const struct super_operations reiserfs_sops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700596 .alloc_inode = reiserfs_alloc_inode,
597 .destroy_inode = reiserfs_destroy_inode,
598 .write_inode = reiserfs_write_inode,
599 .dirty_inode = reiserfs_dirty_inode,
600 .delete_inode = reiserfs_delete_inode,
601 .clear_inode = reiserfs_clear_inode,
602 .put_super = reiserfs_put_super,
603 .write_super = reiserfs_write_super,
604 .sync_fs = reiserfs_sync_fs,
605 .write_super_lockfs = reiserfs_write_super_lockfs,
606 .unlockfs = reiserfs_unlockfs,
607 .statfs = reiserfs_statfs,
608 .remount_fs = reiserfs_remount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700610 .quota_read = reiserfs_quota_read,
611 .quota_write = reiserfs_quota_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612#endif
613};
614
615#ifdef CONFIG_QUOTA
616#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
617
618static int reiserfs_dquot_initialize(struct inode *, int);
619static int reiserfs_dquot_drop(struct inode *);
620static int reiserfs_write_dquot(struct dquot *);
621static int reiserfs_acquire_dquot(struct dquot *);
622static int reiserfs_release_dquot(struct dquot *);
623static int reiserfs_mark_dquot_dirty(struct dquot *);
624static int reiserfs_write_info(struct super_block *, int);
625static int reiserfs_quota_on(struct super_block *, int, int, char *);
626
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700627static struct dquot_operations reiserfs_quota_operations = {
628 .initialize = reiserfs_dquot_initialize,
629 .drop = reiserfs_dquot_drop,
630 .alloc_space = dquot_alloc_space,
631 .alloc_inode = dquot_alloc_inode,
632 .free_space = dquot_free_space,
633 .free_inode = dquot_free_inode,
634 .transfer = dquot_transfer,
635 .write_dquot = reiserfs_write_dquot,
636 .acquire_dquot = reiserfs_acquire_dquot,
637 .release_dquot = reiserfs_release_dquot,
638 .mark_dirty = reiserfs_mark_dquot_dirty,
639 .write_info = reiserfs_write_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700642static struct quotactl_ops reiserfs_qctl_operations = {
643 .quota_on = reiserfs_quota_on,
644 .quota_off = vfs_quota_off,
645 .quota_sync = vfs_quota_sync,
646 .get_info = vfs_get_dqinfo,
647 .set_info = vfs_set_dqinfo,
648 .get_dqblk = vfs_get_dqblk,
649 .set_dqblk = vfs_set_dqblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650};
651#endif
652
653static struct export_operations reiserfs_export_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700654 .encode_fh = reiserfs_encode_fh,
655 .decode_fh = reiserfs_decode_fh,
656 .get_parent = reiserfs_get_parent,
657 .get_dentry = reiserfs_get_dentry,
658};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660/* this struct is used in reiserfs_getopt () for containing the value for those
661 mount options that have values rather than being toggles. */
662typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700663 char *value;
664 int setmask; /* bitmask which is to set on mount_options bitmask when this
665 value is found, 0 is no bits are to be changed. */
666 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
667 value is found, 0 is no bits are to be changed. This is
668 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669} arg_desc_t;
670
671/* Set this bit in arg_required to allow empty arguments */
672#define REISERFS_OPT_ALLOWEMPTY 31
673
674/* this struct is used in reiserfs_getopt() for describing the set of reiserfs
675 mount options */
676typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700677 char *option_name;
678 int arg_required; /* 0 if argument is not required, not 0 otherwise */
679 const arg_desc_t *values; /* list of values accepted by an option */
680 int setmask; /* bitmask which is to set on mount_options bitmask when this
681 value is found, 0 is no bits are to be changed. */
682 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
683 value is found, 0 is no bits are to be changed. This is
684 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685} opt_desc_t;
686
687/* possible values for -o data= */
688static const arg_desc_t logging_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700689 {"ordered", 1 << REISERFS_DATA_ORDERED,
690 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
691 {"journal", 1 << REISERFS_DATA_LOG,
692 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
693 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
694 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800695 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696};
697
698/* possible values for -o barrier= */
699static const arg_desc_t barrier_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
701 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800702 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703};
704
705/* possible values for "-o block-allocator=" and bits which are to be set in
706 s_mount_opt of reiserfs specific part of in-core super block */
707static const arg_desc_t balloc[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700708 {"noborder", 1 << REISERFS_NO_BORDER, 0},
709 {"border", 0, 1 << REISERFS_NO_BORDER},
710 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
711 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
712 {"test4", 1 << REISERFS_TEST4, 0},
713 {"notest4", 0, 1 << REISERFS_TEST4},
714 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715};
716
717static const arg_desc_t tails[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700718 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
719 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
720 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
721 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722};
723
724static const arg_desc_t error_actions[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700725 {"panic", 1 << REISERFS_ERROR_PANIC,
726 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
727 {"ro-remount", 1 << REISERFS_ERROR_RO,
728 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700730 {"continue", 1 << REISERFS_ERROR_CONTINUE,
731 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700733 {NULL, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734};
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/* proceed only one option from a list *cur - string containing of mount options
737 opts - array of options which are accepted
738 opt_arg - if option is found and requires an argument and if it is specifed
739 in the input - pointer to the argument is stored here
740 bit_flags - if option requires to set a certain bit - it is set here
741 return -1 if unknown option is found, opt->arg_required otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700742static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
743 char **opt_arg, unsigned long *bit_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700745 char *p;
746 /* foo=bar,
747 ^ ^ ^
748 | | +-- option_end
749 | +-- arg_start
750 +-- option_start
751 */
752 const opt_desc_t *opt;
753 const arg_desc_t *arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700755 p = *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700757 /* assume argument cannot contain commas */
758 *cur = strchr(p, ',');
759 if (*cur) {
760 *(*cur) = '\0';
761 (*cur)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700764 if (!strncmp(p, "alloc=", 6)) {
765 /* Ugly special case, probably we should redo options parser so that
766 it can understand several arguments for some options, also so that
767 it can fill several bitfields with option values. */
768 if (reiserfs_parse_alloc_options(s, p + 6)) {
769 return -1;
770 } else {
771 return 0;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774
775 /* for every option in the list */
776 for (opt = opts; opt->option_name; opt++) {
777 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
778 if (bit_flags) {
779 if (opt->clrmask ==
780 (1 << REISERFS_UNSUPPORTED_OPT))
781 reiserfs_warning(s, "%s not supported.",
782 p);
783 else
784 *bit_flags &= ~opt->clrmask;
785 if (opt->setmask ==
786 (1 << REISERFS_UNSUPPORTED_OPT))
787 reiserfs_warning(s, "%s not supported.",
788 p);
789 else
790 *bit_flags |= opt->setmask;
791 }
792 break;
793 }
794 }
795 if (!opt->option_name) {
796 reiserfs_warning(s, "unknown mount option \"%s\"", p);
797 return -1;
798 }
799
800 p += strlen(opt->option_name);
801 switch (*p) {
802 case '=':
803 if (!opt->arg_required) {
804 reiserfs_warning(s,
805 "the option \"%s\" does not require an argument",
806 opt->option_name);
807 return -1;
808 }
809 break;
810
811 case 0:
812 if (opt->arg_required) {
813 reiserfs_warning(s,
814 "the option \"%s\" requires an argument",
815 opt->option_name);
816 return -1;
817 }
818 break;
819 default:
820 reiserfs_warning(s, "head of option \"%s\" is only correct",
821 opt->option_name);
822 return -1;
823 }
824
825 /* move to the argument, or to next option if argument is not required */
826 p++;
827
828 if (opt->arg_required
829 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
830 && !strlen(p)) {
831 /* this catches "option=," if not allowed */
832 reiserfs_warning(s, "empty argument for \"%s\"",
833 opt->option_name);
834 return -1;
835 }
836
837 if (!opt->values) {
838 /* *=NULLopt_arg contains pointer to argument */
839 *opt_arg = p;
840 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
841 }
842
843 /* values possible for this option are listed in opt->values */
844 for (arg = opt->values; arg->value; arg++) {
845 if (!strcmp(p, arg->value)) {
846 if (bit_flags) {
847 *bit_flags &= ~arg->clrmask;
848 *bit_flags |= arg->setmask;
849 }
850 return opt->arg_required;
851 }
852 }
853
854 reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
855 opt->option_name);
856 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/* returns 0 if something is wrong in option string, 1 - otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700860static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
861 unsigned long *mount_options,
862 /* after the parsing phase, contains the
863 collection of bitflags defining what
864 mount options were selected. */
865 unsigned long *blocks, /* strtol-ed from NNN of resize=NNN */
866 char **jdev_name,
867 unsigned int *commit_max_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700869 int c;
870 char *arg = NULL;
871 char *pos;
872 opt_desc_t opts[] = {
873 /* Compatibility stuff, so that -o notail for old setups still work */
874 {"tails",.arg_required = 't',.values = tails},
875 {"notail",.clrmask =
876 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
877 {"conv",.setmask = 1 << REISERFS_CONVERT},
878 {"attrs",.setmask = 1 << REISERFS_ATTRS},
879 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700881 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
882 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700884 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
885 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886#endif
887#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700888 {"acl",.setmask = 1 << REISERFS_POSIXACL},
889 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700891 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
892 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893#endif
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800894 {.option_name = "nolog"},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700895 {"replayonly",.setmask = 1 << REPLAYONLY},
896 {"block-allocator",.arg_required = 'a',.values = balloc},
897 {"data",.arg_required = 'd',.values = logging_mode},
898 {"barrier",.arg_required = 'b',.values = barrier_mode},
899 {"resize",.arg_required = 'r',.values = NULL},
900 {"jdev",.arg_required = 'j',.values = NULL},
901 {"nolargeio",.arg_required = 'w',.values = NULL},
902 {"commit",.arg_required = 'c',.values = NULL},
903 {"usrquota",.setmask = 1 << REISERFS_QUOTA},
904 {"grpquota",.setmask = 1 << REISERFS_QUOTA},
905 {"noquota",.clrmask = 1 << REISERFS_QUOTA},
906 {"errors",.arg_required = 'e',.values = error_actions},
907 {"usrjquota",.arg_required =
908 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
909 {"grpjquota",.arg_required =
910 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
911 {"jqfmt",.arg_required = 'f',.values = NULL},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800912 {.option_name = NULL}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700913 };
914
915 *blocks = 0;
916 if (!options || !*options)
917 /* use default configuration: create tails, journaling on, no
918 conversion to newest format */
919 return 1;
920
921 for (pos = options; pos;) {
922 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
923 if (c == -1)
924 /* wrong option is given */
925 return 0;
926
927 if (c == 'r') {
928 char *p;
929
930 p = NULL;
931 /* "resize=NNN" or "resize=auto" */
932
933 if (!strcmp(arg, "auto")) {
934 /* From JFS code, to auto-get the size. */
935 *blocks =
936 s->s_bdev->bd_inode->i_size >> s->
937 s_blocksize_bits;
938 } else {
939 *blocks = simple_strtoul(arg, &p, 0);
940 if (*p != '\0') {
941 /* NNN does not look like a number */
942 reiserfs_warning(s,
943 "reiserfs_parse_options: bad value %s",
944 arg);
945 return 0;
946 }
947 }
948 }
949
950 if (c == 'c') {
951 char *p = NULL;
952 unsigned long val = simple_strtoul(arg, &p, 0);
953 /* commit=NNN (time in seconds) */
954 if (*p != '\0' || val >= (unsigned int)-1) {
955 reiserfs_warning(s,
956 "reiserfs_parse_options: bad value %s",
957 arg);
958 return 0;
959 }
960 *commit_max_age = (unsigned int)val;
961 }
962
963 if (c == 'w') {
Adrian Bunk36b756f2006-09-27 01:50:50 -0700964 reiserfs_warning(s, "reiserfs: nolargeio option is no longer supported");
965 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700966 }
967
968 if (c == 'j') {
969 if (arg && *arg && jdev_name) {
970 if (*jdev_name) { //Hm, already assigned?
971 reiserfs_warning(s,
972 "reiserfs_parse_options: journal device was already specified to be %s",
973 *jdev_name);
974 return 0;
975 }
976 *jdev_name = arg;
977 }
978 }
979#ifdef CONFIG_QUOTA
980 if (c == 'u' || c == 'g') {
981 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
982
983 if (sb_any_quota_enabled(s)) {
984 reiserfs_warning(s,
985 "reiserfs_parse_options: cannot change journalled quota options when quota turned on.");
986 return 0;
987 }
988 if (*arg) { /* Some filename specified? */
989 if (REISERFS_SB(s)->s_qf_names[qtype]
990 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
991 arg)) {
992 reiserfs_warning(s,
993 "reiserfs_parse_options: %s quota file already specified.",
994 QTYPE2NAME(qtype));
995 return 0;
996 }
997 if (strchr(arg, '/')) {
998 reiserfs_warning(s,
999 "reiserfs_parse_options: quotafile must be on filesystem root.");
1000 return 0;
1001 }
1002 REISERFS_SB(s)->s_qf_names[qtype] =
1003 kmalloc(strlen(arg) + 1, GFP_KERNEL);
1004 if (!REISERFS_SB(s)->s_qf_names[qtype]) {
1005 reiserfs_warning(s,
1006 "reiserfs_parse_options: not enough memory for storing quotafile name.");
1007 return 0;
1008 }
1009 strcpy(REISERFS_SB(s)->s_qf_names[qtype], arg);
1010 *mount_options |= 1 << REISERFS_QUOTA;
1011 } else {
James Lamanna833d3042005-10-30 15:00:16 -08001012 kfree(REISERFS_SB(s)->s_qf_names[qtype]);
1013 REISERFS_SB(s)->s_qf_names[qtype] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001014 }
1015 }
1016 if (c == 'f') {
1017 if (!strcmp(arg, "vfsold"))
1018 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_OLD;
1019 else if (!strcmp(arg, "vfsv0"))
1020 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_V0;
1021 else {
1022 reiserfs_warning(s,
1023 "reiserfs_parse_options: unknown quota format specified.");
1024 return 0;
1025 }
1026 }
1027#else
1028 if (c == 'u' || c == 'g' || c == 'f') {
1029 reiserfs_warning(s,
1030 "reiserfs_parse_options: journalled quota options not supported.");
1031 return 0;
1032 }
1033#endif
1034 }
1035
1036#ifdef CONFIG_QUOTA
1037 if (!REISERFS_SB(s)->s_jquota_fmt
1038 && (REISERFS_SB(s)->s_qf_names[USRQUOTA]
1039 || REISERFS_SB(s)->s_qf_names[GRPQUOTA])) {
1040 reiserfs_warning(s,
1041 "reiserfs_parse_options: journalled quota format not specified.");
1042 return 0;
1043 }
1044 /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
1045 if (!(*mount_options & (1 << REISERFS_QUOTA))
1046 && sb_any_quota_enabled(s)) {
1047 reiserfs_warning(s,
1048 "reiserfs_parse_options: quota options must be present when quota is turned on.");
1049 return 0;
1050 }
1051#endif
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001056static void switch_data_mode(struct super_block *s, unsigned long mode)
1057{
1058 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1059 (1 << REISERFS_DATA_ORDERED) |
1060 (1 << REISERFS_DATA_WRITEBACK));
1061 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1065{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001066 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1067 if (!reiserfs_data_log(s)) {
1068 switch_data_mode(s, REISERFS_DATA_LOG);
1069 reiserfs_info(s, "switching to journaled data mode\n");
1070 }
1071 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1072 if (!reiserfs_data_ordered(s)) {
1073 switch_data_mode(s, REISERFS_DATA_ORDERED);
1074 reiserfs_info(s, "switching to ordered data mode\n");
1075 }
1076 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1077 if (!reiserfs_data_writeback(s)) {
1078 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1079 reiserfs_info(s, "switching to writeback data mode\n");
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001084static void handle_barrier_mode(struct super_block *s, unsigned long bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001086 int flush = (1 << REISERFS_BARRIER_FLUSH);
1087 int none = (1 << REISERFS_BARRIER_NONE);
1088 int all_barrier = flush | none;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001090 if (bits & all_barrier) {
1091 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1092 if (bits & flush) {
1093 REISERFS_SB(s)->s_mount_opt |= flush;
1094 printk("reiserfs: enabling write barrier flush mode\n");
1095 } else if (bits & none) {
1096 REISERFS_SB(s)->s_mount_opt |= none;
1097 printk("reiserfs: write barriers turned off\n");
1098 }
1099 }
1100}
1101
1102static void handle_attrs(struct super_block *s)
1103{
1104 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1105
1106 if (reiserfs_attrs(s)) {
1107 if (old_format_only(s)) {
1108 reiserfs_warning(s,
1109 "reiserfs: cannot support attributes on 3.5.x disk format");
1110 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return;
1112 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001113 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1114 reiserfs_warning(s,
1115 "reiserfs: cannot support attributes until flag is set in super-block");
1116 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 }
1119}
1120
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001121static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001123 struct reiserfs_super_block *rs;
1124 struct reiserfs_transaction_handle th;
1125 unsigned long blocks;
1126 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1127 unsigned long safe_mask = 0;
1128 unsigned int commit_max_age = (unsigned int)-1;
1129 struct reiserfs_journal *journal = SB_JOURNAL(s);
1130 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001132 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133#endif
1134
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001135 rs = SB_DISK_SUPER_BLOCK(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001137 if (!reiserfs_parse_options
1138 (s, arg, &mount_options, &blocks, NULL, &commit_max_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139#ifdef CONFIG_QUOTA
James Lamanna833d3042005-10-30 15:00:16 -08001140 for (i = 0; i < MAXQUOTAS; i++) {
1141 kfree(REISERFS_SB(s)->s_qf_names[i]);
1142 REISERFS_SB(s)->s_qf_names[i] = NULL;
1143 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001144#endif
1145 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001148 handle_attrs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001150 /* Add options that are safe here */
1151 safe_mask |= 1 << REISERFS_SMALLTAIL;
1152 safe_mask |= 1 << REISERFS_LARGETAIL;
1153 safe_mask |= 1 << REISERFS_NO_BORDER;
1154 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1155 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1156 safe_mask |= 1 << REISERFS_TEST4;
1157 safe_mask |= 1 << REISERFS_ATTRS;
1158 safe_mask |= 1 << REISERFS_XATTRS_USER;
1159 safe_mask |= 1 << REISERFS_POSIXACL;
1160 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1161 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1162 safe_mask |= 1 << REISERFS_ERROR_RO;
1163 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1164 safe_mask |= 1 << REISERFS_ERROR_PANIC;
1165 safe_mask |= 1 << REISERFS_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001167 /* Update the bitmask, taking care to keep
1168 * the bits we're not allowed to change here */
1169 REISERFS_SB(s)->s_mount_opt =
1170 (REISERFS_SB(s)->
1171 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001173 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1174 journal->j_max_commit_age = commit_max_age;
1175 journal->j_max_trans_age = commit_max_age;
1176 } else if (commit_max_age == 0) {
1177 /* 0 means restore defaults. */
1178 journal->j_max_commit_age = journal->j_default_max_commit_age;
1179 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001182 if (blocks) {
1183 int rc = reiserfs_resize(s, blocks);
1184 if (rc != 0)
1185 return rc;
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001188 if (*mount_flags & MS_RDONLY) {
1189 reiserfs_xattr_init(s, *mount_flags);
1190 /* remount read-only */
1191 if (s->s_flags & MS_RDONLY)
1192 /* it is read-only already */
1193 return 0;
1194 /* try to remount file system with read-only permissions */
1195 if (sb_umount_state(rs) == REISERFS_VALID_FS
1196 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1197 return 0;
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001200 err = journal_begin(&th, s, 10);
1201 if (err)
1202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001204 /* Mounting a rw partition read-only. */
1205 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1206 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1207 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1208 } else {
1209 /* remount read-write */
1210 if (!(s->s_flags & MS_RDONLY)) {
1211 reiserfs_xattr_init(s, *mount_flags);
1212 return 0; /* We are read-write already */
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001215 if (reiserfs_is_journal_aborted(journal))
1216 return journal->j_errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001218 handle_data_mode(s, mount_options);
1219 handle_barrier_mode(s, mount_options);
1220 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1221 s->s_flags &= ~MS_RDONLY; /* now it is safe to call journal_begin */
1222 err = journal_begin(&th, s, 10);
1223 if (err)
1224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001226 /* Mount a partition which is read-only, read-write */
1227 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1228 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1229 s->s_flags &= ~MS_RDONLY;
1230 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1231 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1232 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1233 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1234 }
1235 /* this will force a full flush of all journal lists */
1236 SB_JOURNAL(s)->j_must_wait = 1;
1237 err = journal_end(&th, s, 10);
1238 if (err)
1239 return err;
1240 s->s_dirt = 0;
1241
1242 if (!(*mount_flags & MS_RDONLY)) {
1243 finish_unfinished(s);
1244 reiserfs_xattr_init(s, *mount_flags);
1245 }
1246
1247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001250static int read_super_block(struct super_block *s, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001252 struct buffer_head *bh;
1253 struct reiserfs_super_block *rs;
1254 int fs_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001256 bh = sb_bread(s, offset / s->s_blocksize);
1257 if (!bh) {
1258 reiserfs_warning(s, "sh-2006: read_super_block: "
1259 "bread failed (dev %s, block %lu, size %lu)",
1260 reiserfs_bdevname(s), offset / s->s_blocksize,
1261 s->s_blocksize);
1262 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265 rs = (struct reiserfs_super_block *)bh->b_data;
1266 if (!is_any_reiserfs_magic_string(rs)) {
1267 brelse(bh);
1268 return 1;
1269 }
1270 //
1271 // ok, reiserfs signature (old or new) found in at the given offset
1272 //
1273 fs_blocksize = sb_blocksize(rs);
1274 brelse(bh);
1275 sb_set_blocksize(s, fs_blocksize);
1276
1277 bh = sb_bread(s, offset / s->s_blocksize);
1278 if (!bh) {
1279 reiserfs_warning(s, "sh-2007: read_super_block: "
1280 "bread failed (dev %s, block %lu, size %lu)\n",
1281 reiserfs_bdevname(s), offset / s->s_blocksize,
1282 s->s_blocksize);
1283 return 1;
1284 }
1285
1286 rs = (struct reiserfs_super_block *)bh->b_data;
1287 if (sb_blocksize(rs) != s->s_blocksize) {
1288 reiserfs_warning(s, "sh-2011: read_super_block: "
1289 "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1290 reiserfs_bdevname(s),
1291 (unsigned long long)bh->b_blocknr,
1292 s->s_blocksize);
1293 brelse(bh);
1294 return 1;
1295 }
1296
1297 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1298 brelse(bh);
1299 reiserfs_warning(s,
1300 "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1301 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1302 "get newer reiserfsprogs package");
1303 return 1;
1304 }
1305
1306 SB_BUFFER_WITH_SB(s) = bh;
1307 SB_DISK_SUPER_BLOCK(s) = rs;
1308
1309 if (is_reiserfs_jr(rs)) {
1310 /* magic is of non-standard journal filesystem, look at s_version to
1311 find which format is in use */
1312 if (sb_version(rs) == REISERFS_VERSION_2)
1313 reiserfs_warning(s,
1314 "read_super_block: found reiserfs format \"3.6\""
1315 " with non-standard journal");
1316 else if (sb_version(rs) == REISERFS_VERSION_1)
1317 reiserfs_warning(s,
1318 "read_super_block: found reiserfs format \"3.5\""
1319 " with non-standard journal");
1320 else {
1321 reiserfs_warning(s,
1322 "sh-2012: read_super_block: found unknown "
1323 "format \"%u\" of reiserfs with non-standard magic",
1324 sb_version(rs));
1325 return 1;
1326 }
1327 } else
1328 /* s_version of standard format may contain incorrect information,
1329 so we just look at the magic string */
1330 reiserfs_info(s,
1331 "found reiserfs format \"%s\" with standard journal\n",
1332 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1333
1334 s->s_op = &reiserfs_sops;
1335 s->s_export_op = &reiserfs_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001337 s->s_qcop = &reiserfs_qctl_operations;
1338 s->dq_op = &reiserfs_quota_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339#endif
1340
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001341 /* new format is limited by the 32 bit wide i_blocks field, want to
1342 ** be one full block below that.
1343 */
1344 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348/* after journal replay, reread all bitmap and super blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001349static int reread_meta_blocks(struct super_block *s)
1350{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001351 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
1352 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1353 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1354 reiserfs_warning(s,
1355 "reread_meta_blocks, error reading the super");
1356 return 1;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362/////////////////////////////////////////////////////
1363// hash detection stuff
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365// if root directory is empty - we set default - Yura's - hash and
1366// warn about it
1367// FIXME: we look for only one name in a directory. If tea and yura
1368// bith have the same value - we ask user to send report to the
1369// mailing list
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001370static __u32 find_hash_out(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001372 int retval;
1373 struct inode *inode;
1374 struct cpu_key key;
1375 INITIALIZE_PATH(path);
1376 struct reiserfs_dir_entry de;
1377 __u32 hash = DEFAULT_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001379 inode = s->s_root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001381 do { // Some serious "goto"-hater was there ;)
1382 u32 teahash, r5hash, yurahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001384 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1385 retval = search_by_entry_key(s, &key, &path, &de);
1386 if (retval == IO_ERROR) {
1387 pathrelse(&path);
1388 return UNSET_HASH;
1389 }
1390 if (retval == NAME_NOT_FOUND)
1391 de.de_entry_num--;
1392 set_de_name_and_namelen(&de);
1393 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1394 /* allow override in this case */
1395 if (reiserfs_rupasov_hash(s)) {
1396 hash = YURA_HASH;
1397 }
1398 reiserfs_warning(s, "FS seems to be empty, autodetect "
1399 "is using the default hash");
1400 break;
1401 }
1402 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1403 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1404 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1405 if (((teahash == r5hash)
1406 &&
1407 (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1408 == r5hash)) || ((teahash == yurahash)
1409 && (yurahash ==
1410 GET_HASH_VALUE(deh_offset
1411 (&
1412 (de.
1413 de_deh[de.
1414 de_entry_num])))))
1415 || ((r5hash == yurahash)
1416 && (yurahash ==
1417 GET_HASH_VALUE(deh_offset
1418 (&(de.de_deh[de.de_entry_num])))))) {
1419 reiserfs_warning(s,
1420 "Unable to automatically detect hash function. "
1421 "Please mount with -o hash={tea,rupasov,r5}",
1422 reiserfs_bdevname(s));
1423 hash = UNSET_HASH;
1424 break;
1425 }
1426 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1427 yurahash)
1428 hash = YURA_HASH;
1429 else if (GET_HASH_VALUE
1430 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1431 hash = TEA_HASH;
1432 else if (GET_HASH_VALUE
1433 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1434 hash = R5_HASH;
1435 else {
1436 reiserfs_warning(s, "Unrecognised hash function");
1437 hash = UNSET_HASH;
1438 }
1439 } while (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001441 pathrelse(&path);
1442 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445// finds out which hash names are sorted with
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001446static int what_hash(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001448 __u32 code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001450 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001452 /* reiserfs_hash_detect() == true if any of the hash mount options
1453 ** were used. We must check them to make sure the user isn't
1454 ** using a bad hash value
1455 */
1456 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1457 code = find_hash_out(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001459 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1460 /* detection has found the hash, and we must check against the
1461 ** mount options
1462 */
1463 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1464 reiserfs_warning(s, "Error, %s hash detected, "
1465 "unable to force rupasov hash",
1466 reiserfs_hashname(code));
1467 code = UNSET_HASH;
1468 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1469 reiserfs_warning(s, "Error, %s hash detected, "
1470 "unable to force tea hash",
1471 reiserfs_hashname(code));
1472 code = UNSET_HASH;
1473 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1474 reiserfs_warning(s, "Error, %s hash detected, "
1475 "unable to force r5 hash",
1476 reiserfs_hashname(code));
1477 code = UNSET_HASH;
1478 }
1479 } else {
1480 /* find_hash_out was not called or could not determine the hash */
1481 if (reiserfs_rupasov_hash(s)) {
1482 code = YURA_HASH;
1483 } else if (reiserfs_tea_hash(s)) {
1484 code = TEA_HASH;
1485 } else if (reiserfs_r5_hash(s)) {
1486 code = R5_HASH;
1487 }
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001490 /* if we are mounted RW, and we have a new valid hash code, update
1491 ** the super
1492 */
1493 if (code != UNSET_HASH &&
1494 !(s->s_flags & MS_RDONLY) &&
1495 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1496 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1497 }
1498 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501// return pointer to appropriate function
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001502static hashf_t hash_function(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001504 switch (what_hash(s)) {
1505 case TEA_HASH:
1506 reiserfs_info(s, "Using tea hash to sort names\n");
1507 return keyed_hash;
1508 case YURA_HASH:
1509 reiserfs_info(s, "Using rupasov hash to sort names\n");
1510 return yura_hash;
1511 case R5_HASH:
1512 reiserfs_info(s, "Using r5 hash to sort names\n");
1513 return r5_hash;
1514 }
1515 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
1518// this is used to set up correct value for old partitions
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001519static int function2code(hashf_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001521 if (func == keyed_hash)
1522 return TEA_HASH;
1523 if (func == yura_hash)
1524 return YURA_HASH;
1525 if (func == r5_hash)
1526 return R5_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001528 BUG(); // should never happen
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533#define SWARN(silent, s, ...) \
1534 if (!(silent)) \
1535 reiserfs_warning (s, __VA_ARGS__)
1536
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001537static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001539 struct inode *root_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001540 struct reiserfs_transaction_handle th;
1541 int old_format = 0;
1542 unsigned long blocks;
1543 unsigned int commit_max_age = 0;
1544 int jinit_done = 0;
1545 struct reiserfs_iget_args args;
1546 struct reiserfs_super_block *rs;
1547 char *jdev_name;
1548 struct reiserfs_sb_info *sbi;
1549 int errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Yan Burman01afb212006-12-06 20:39:01 -08001551 sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001552 if (!sbi) {
1553 errval = -ENOMEM;
1554 goto error;
1555 }
1556 s->s_fs_info = sbi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001557 /* Set default values for options: non-aggressive tails, RO on errors */
1558 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1559 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1560 /* no preallocation minimum, be smart in
1561 reiserfs_file_write instead */
1562 REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1563 /* Preallocate by 16 blocks (17-1) at once */
1564 REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001565#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001566 /* Initialize the rwsem for xattr dir */
1567 init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001568#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001569 /* setup default block allocator options */
1570 reiserfs_init_alloc_options(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001572 jdev_name = NULL;
1573 if (reiserfs_parse_options
1574 (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
1575 &commit_max_age) == 0) {
1576 goto error;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001579 if (blocks) {
1580 SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1581 "for remount only");
1582 goto error;
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001585 /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1586 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1587 old_format = 1;
1588 /* try new format (64-th 1k block), which can contain reiserfs super block */
1589 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1590 SWARN(silent, s,
1591 "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
1592 reiserfs_bdevname(s));
1593 goto error;
1594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001596 rs = SB_DISK_SUPER_BLOCK(s);
1597 /* Let's do basic sanity check to verify that underlying device is not
1598 smaller than the filesystem. If the check fails then abort and scream,
1599 because bad stuff will happen otherwise. */
1600 if (s->s_bdev && s->s_bdev->bd_inode
1601 && i_size_read(s->s_bdev->bd_inode) <
1602 sb_block_count(rs) * sb_blocksize(rs)) {
1603 SWARN(silent, s,
1604 "Filesystem on %s cannot be mounted because it is bigger than the device",
1605 reiserfs_bdevname(s));
1606 SWARN(silent, s,
1607 "You may need to run fsck or increase size of your LVM partition");
1608 SWARN(silent, s,
1609 "Or may be you forgot to reboot after fdisk when it told you to");
1610 goto error;
1611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001613 sbi->s_mount_state = SB_REISERFS_STATE(s);
1614 sbi->s_mount_state = REISERFS_VALID_FS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Jeff Mahoney6f010462006-09-30 23:28:43 -07001616 if ((errval = reiserfs_init_bitmap_cache(s))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001617 SWARN(silent, s,
1618 "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1619 goto error;
1620 }
Jeff Mahoneyd2c89a42006-11-02 22:07:20 -08001621 errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001623 SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
1624 SWARN(silent, s, "- it is slow mode for debugging.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625#endif
1626
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001627 /* make data=ordered the default */
1628 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1629 !reiserfs_data_writeback(s)) {
1630 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001633 if (reiserfs_data_log(s)) {
1634 reiserfs_info(s, "using journaled data mode\n");
1635 } else if (reiserfs_data_ordered(s)) {
1636 reiserfs_info(s, "using ordered data mode\n");
1637 } else {
1638 reiserfs_info(s, "using writeback data mode\n");
1639 }
1640 if (reiserfs_barrier_flush(s)) {
1641 printk("reiserfs: using flush barriers\n");
1642 }
1643 // set_device_ro(s->s_dev, 1) ;
1644 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1645 SWARN(silent, s,
1646 "sh-2022: reiserfs_fill_super: unable to initialize journal space");
1647 goto error;
1648 } else {
1649 jinit_done = 1; /* once this is set, journal_release must be called
1650 ** if we error out of the mount
1651 */
1652 }
1653 if (reread_meta_blocks(s)) {
1654 SWARN(silent, s,
1655 "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
1656 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001659 if (replay_only(s))
1660 goto error;
1661
1662 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1663 SWARN(silent, s,
1664 "clm-7000: Detected readonly device, marking FS readonly");
1665 s->s_flags |= MS_RDONLY;
1666 }
1667 args.objectid = REISERFS_ROOT_OBJECTID;
1668 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
1669 root_inode =
1670 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
1671 reiserfs_init_locked_inode, (void *)(&args));
1672 if (!root_inode) {
1673 SWARN(silent, s,
1674 "jmacd-10: reiserfs_fill_super: get root inode failed");
1675 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001678 if (root_inode->i_state & I_NEW) {
1679 reiserfs_read_locked_inode(root_inode, &args);
1680 unlock_new_inode(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001683 s->s_root = d_alloc_root(root_inode);
1684 if (!s->s_root) {
1685 iput(root_inode);
1686 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001688 // define and initialize hash function
1689 sbi->s_hash_function = hash_function(s);
1690 if (sbi->s_hash_function == NULL) {
1691 dput(s->s_root);
1692 s->s_root = NULL;
1693 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001695
1696 if (is_reiserfs_3_5(rs)
1697 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
1698 set_bit(REISERFS_3_5, &(sbi->s_properties));
Jeff Mahoneye1fabd32006-09-30 23:28:40 -07001699 else if (old_format)
1700 set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001701 else
1702 set_bit(REISERFS_3_6, &(sbi->s_properties));
1703
1704 if (!(s->s_flags & MS_RDONLY)) {
1705
1706 errval = journal_begin(&th, s, 1);
1707 if (errval) {
1708 dput(s->s_root);
1709 s->s_root = NULL;
1710 goto error;
1711 }
1712 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1713
1714 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1715 set_sb_fs_state(rs, 0);
1716
1717 if (old_format_only(s)) {
1718 /* filesystem of format 3.5 either with standard or non-standard
1719 journal */
1720 if (convert_reiserfs(s)) {
1721 /* and -o conv is given */
1722 if (!silent)
1723 reiserfs_info(s,
1724 "converting 3.5 filesystem to the 3.6 format");
1725
1726 if (is_reiserfs_3_5(rs))
1727 /* put magic string of 3.6 format. 2.2 will not be able to
1728 mount this filesystem anymore */
1729 memcpy(rs->s_v1.s_magic,
1730 reiserfs_3_6_magic_string,
1731 sizeof
1732 (reiserfs_3_6_magic_string));
1733
1734 set_sb_version(rs, REISERFS_VERSION_2);
1735 reiserfs_convert_objectid_map_v1(s);
1736 set_bit(REISERFS_3_6, &(sbi->s_properties));
1737 clear_bit(REISERFS_3_5, &(sbi->s_properties));
1738 } else if (!silent) {
1739 reiserfs_info(s, "using 3.5.x disk format\n");
1740 }
1741 }
1742
1743 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1744 errval = journal_end(&th, s, 1);
1745 if (errval) {
1746 dput(s->s_root);
1747 s->s_root = NULL;
1748 goto error;
1749 }
1750
1751 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1752 dput(s->s_root);
1753 s->s_root = NULL;
1754 goto error;
1755 }
1756
1757 /* look for files which were to be removed in previous session */
1758 finish_unfinished(s);
1759 } else {
1760 if (old_format_only(s) && !silent) {
1761 reiserfs_info(s, "using 3.5.x disk format\n");
1762 }
1763
1764 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1765 dput(s->s_root);
1766 s->s_root = NULL;
1767 goto error;
1768 }
1769 }
1770 // mark hash in super block: it could be unset. overwrite should be ok
1771 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
1772
1773 handle_attrs(s);
1774
1775 reiserfs_proc_info_init(s);
1776
1777 init_waitqueue_head(&(sbi->s_wait));
1778 spin_lock_init(&sbi->bitmap_lock);
1779
1780 return (0);
1781
1782 error:
1783 if (jinit_done) { /* kill the commit thread, free journal ram */
1784 journal_release_error(NULL, s);
1785 }
Jeff Mahoney50652272006-09-30 23:28:44 -07001786
1787 reiserfs_free_bitmap_cache(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001788 if (SB_BUFFER_WITH_SB(s))
1789 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790#ifdef CONFIG_QUOTA
Jeff Mahoney50652272006-09-30 23:28:44 -07001791 {
1792 int j;
1793 for (j = 0; j < MAXQUOTAS; j++) {
1794 kfree(sbi->s_qf_names[j]);
1795 sbi->s_qf_names[j] = NULL;
1796 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798#endif
James Lamanna833d3042005-10-30 15:00:16 -08001799 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001801 s->s_fs_info = NULL;
1802 return errval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
David Howells726c3342006-06-23 02:02:58 -07001805static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
David Howells726c3342006-06-23 02:02:58 -07001807 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001808
1809 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
1810 buf->f_bfree = sb_free_blocks(rs);
1811 buf->f_bavail = buf->f_bfree;
1812 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
David Howells726c3342006-06-23 02:02:58 -07001813 buf->f_bsize = dentry->d_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001814 /* changed to accommodate gcc folks. */
1815 buf->f_type = REISERFS_SUPER_MAGIC;
1816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
1819#ifdef CONFIG_QUOTA
1820static int reiserfs_dquot_initialize(struct inode *inode, int type)
1821{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001822 struct reiserfs_transaction_handle th;
1823 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001825 /* We may create quota structure so we need to reserve enough blocks */
1826 reiserfs_write_lock(inode->i_sb);
1827 ret =
1828 journal_begin(&th, inode->i_sb,
1829 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1830 if (ret)
1831 goto out;
1832 ret = dquot_initialize(inode, type);
1833 err =
1834 journal_end(&th, inode->i_sb,
1835 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1836 if (!ret && err)
1837 ret = err;
1838 out:
1839 reiserfs_write_unlock(inode->i_sb);
1840 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843static int reiserfs_dquot_drop(struct inode *inode)
1844{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001845 struct reiserfs_transaction_handle th;
1846 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001848 /* We may delete quota structure so we need to reserve enough blocks */
1849 reiserfs_write_lock(inode->i_sb);
1850 ret =
1851 journal_begin(&th, inode->i_sb,
1852 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1853 if (ret)
1854 goto out;
1855 ret = dquot_drop(inode);
1856 err =
1857 journal_end(&th, inode->i_sb,
1858 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1859 if (!ret && err)
1860 ret = err;
1861 out:
1862 reiserfs_write_unlock(inode->i_sb);
1863 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
1866static int reiserfs_write_dquot(struct dquot *dquot)
1867{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001868 struct reiserfs_transaction_handle th;
1869 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001871 reiserfs_write_lock(dquot->dq_sb);
1872 ret =
1873 journal_begin(&th, dquot->dq_sb,
1874 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1875 if (ret)
1876 goto out;
1877 ret = dquot_commit(dquot);
1878 err =
1879 journal_end(&th, dquot->dq_sb,
1880 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1881 if (!ret && err)
1882 ret = err;
1883 out:
1884 reiserfs_write_unlock(dquot->dq_sb);
1885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
1888static int reiserfs_acquire_dquot(struct dquot *dquot)
1889{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001890 struct reiserfs_transaction_handle th;
1891 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001893 reiserfs_write_lock(dquot->dq_sb);
1894 ret =
1895 journal_begin(&th, dquot->dq_sb,
1896 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1897 if (ret)
1898 goto out;
1899 ret = dquot_acquire(dquot);
1900 err =
1901 journal_end(&th, dquot->dq_sb,
1902 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1903 if (!ret && err)
1904 ret = err;
1905 out:
1906 reiserfs_write_unlock(dquot->dq_sb);
1907 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910static int reiserfs_release_dquot(struct dquot *dquot)
1911{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001912 struct reiserfs_transaction_handle th;
1913 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001915 reiserfs_write_lock(dquot->dq_sb);
1916 ret =
1917 journal_begin(&th, dquot->dq_sb,
1918 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1919 if (ret)
1920 goto out;
1921 ret = dquot_release(dquot);
1922 err =
1923 journal_end(&th, dquot->dq_sb,
1924 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
1925 if (!ret && err)
1926 ret = err;
1927 out:
1928 reiserfs_write_unlock(dquot->dq_sb);
1929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
1932static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
1933{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001934 /* Are we journalling quotas? */
1935 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
1936 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
1937 dquot_mark_dquot_dirty(dquot);
1938 return reiserfs_write_dquot(dquot);
1939 } else
1940 return dquot_mark_dquot_dirty(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
1943static int reiserfs_write_info(struct super_block *sb, int type)
1944{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001945 struct reiserfs_transaction_handle th;
1946 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001948 /* Data block + inode block */
1949 reiserfs_write_lock(sb);
1950 ret = journal_begin(&th, sb, 2);
1951 if (ret)
1952 goto out;
1953 ret = dquot_commit_info(sb, type);
1954 err = journal_end(&th, sb, 2);
1955 if (!ret && err)
1956 ret = err;
1957 out:
1958 reiserfs_write_unlock(sb);
1959 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962/*
Christoph Hellwig84de8562005-06-23 00:09:16 -07001963 * Turn on quotas during mount time - we need to find the quota file and such...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
1965static int reiserfs_quota_on_mount(struct super_block *sb, int type)
1966{
Christoph Hellwig84de8562005-06-23 00:09:16 -07001967 return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001968 REISERFS_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
1971/*
1972 * Standard function to be called on quota_on
1973 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001974static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
1975 char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001977 int err;
1978 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001980 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
1981 return -EINVAL;
1982 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
1983 if (err)
1984 return err;
1985 /* Quotafile not on the same filesystem? */
1986 if (nd.mnt->mnt_sb != sb) {
1987 path_release(&nd);
1988 return -EXDEV;
1989 }
1990 /* We must not pack tails for quota files on reiserfs for quota IO to work */
1991 if (!REISERFS_I(nd.dentry->d_inode)->i_flags & i_nopack_mask) {
1992 reiserfs_warning(sb,
1993 "reiserfs: Quota file must have tail packing disabled.");
1994 path_release(&nd);
1995 return -EINVAL;
1996 }
1997 /* Not journalling quota? No more tests needed... */
1998 if (!REISERFS_SB(sb)->s_qf_names[USRQUOTA] &&
1999 !REISERFS_SB(sb)->s_qf_names[GRPQUOTA]) {
2000 path_release(&nd);
2001 return vfs_quota_on(sb, type, format_id, path);
2002 }
2003 /* Quotafile not of fs root? */
2004 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2005 reiserfs_warning(sb,
2006 "reiserfs: Quota file not on filesystem root. "
2007 "Journalled quota will not work.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 path_release(&nd);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002009 return vfs_quota_on(sb, type, format_id, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
2012/* Read data from quotafile - avoid pagecache and such because we cannot afford
2013 * acquiring the locks... As quota files are never truncated and quota code
2014 * itself serializes the operations (and noone else should touch the files)
2015 * we don't have to be afraid of races */
2016static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2017 size_t len, loff_t off)
2018{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002019 struct inode *inode = sb_dqopt(sb)->files[type];
2020 unsigned long blk = off >> sb->s_blocksize_bits;
2021 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2022 size_t toread;
2023 struct buffer_head tmp_bh, *bh;
2024 loff_t i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002026 if (off > i_size)
2027 return 0;
2028 if (off + len > i_size)
2029 len = i_size - off;
2030 toread = len;
2031 while (toread > 0) {
2032 tocopy =
2033 sb->s_blocksize - offset <
2034 toread ? sb->s_blocksize - offset : toread;
2035 tmp_bh.b_state = 0;
2036 /* Quota files are without tails so we can safely use this function */
2037 reiserfs_write_lock(sb);
2038 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2039 reiserfs_write_unlock(sb);
2040 if (err)
2041 return err;
2042 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2043 memset(data, 0, tocopy);
2044 else {
2045 bh = sb_bread(sb, tmp_bh.b_blocknr);
2046 if (!bh)
2047 return -EIO;
2048 memcpy(data, bh->b_data + offset, tocopy);
2049 brelse(bh);
2050 }
2051 offset = 0;
2052 toread -= tocopy;
2053 data += tocopy;
2054 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002056 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
2059/* Write to quotafile (we know the transaction is already started and has
2060 * enough credits) */
2061static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2062 const char *data, size_t len, loff_t off)
2063{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002064 struct inode *inode = sb_dqopt(sb)->files[type];
2065 unsigned long blk = off >> sb->s_blocksize_bits;
2066 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2067 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2068 size_t towrite = len;
2069 struct buffer_head tmp_bh, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Arjan van de Ven5c81a412006-07-03 00:25:20 -07002071 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002072 while (towrite > 0) {
2073 tocopy = sb->s_blocksize - offset < towrite ?
2074 sb->s_blocksize - offset : towrite;
2075 tmp_bh.b_state = 0;
2076 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2077 if (err)
2078 goto out;
2079 if (offset || tocopy != sb->s_blocksize)
2080 bh = sb_bread(sb, tmp_bh.b_blocknr);
2081 else
2082 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2083 if (!bh) {
2084 err = -EIO;
2085 goto out;
2086 }
2087 lock_buffer(bh);
2088 memcpy(bh->b_data + offset, data, tocopy);
2089 flush_dcache_page(bh->b_page);
2090 set_buffer_uptodate(bh);
2091 unlock_buffer(bh);
2092 reiserfs_prepare_for_journal(sb, bh, 1);
2093 journal_mark_dirty(current->journal_info, sb, bh);
2094 if (!journal_quota)
2095 reiserfs_add_ordered_list(inode, bh);
2096 brelse(bh);
2097 offset = 0;
2098 towrite -= tocopy;
2099 data += tocopy;
2100 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002102 out:
2103 if (len == towrite)
2104 return err;
2105 if (inode->i_size < off + len - towrite)
2106 i_size_write(inode, off + len - towrite);
2107 inode->i_version++;
2108 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2109 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002110 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002111 return len - towrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
2114#endif
2115
David Howells454e2392006-06-23 02:02:57 -07002116static int get_super_block(struct file_system_type *fs_type,
2117 int flags, const char *dev_name,
2118 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
David Howells454e2392006-06-23 02:02:57 -07002120 return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
2121 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002124static int __init init_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 int ret;
2127
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002128 if ((ret = init_inodecache())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return ret;
2130 }
2131
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002132 if ((ret = reiserfs_xattr_register_handlers()))
2133 goto failed_reiserfs_xattr_register_handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002135 reiserfs_proc_info_global_init();
2136 reiserfs_proc_register_global("version",
2137 reiserfs_global_version_in_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002139 ret = register_filesystem(&reiserfs_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (ret == 0) {
2142 return 0;
2143 }
2144
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002145 reiserfs_xattr_unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002147 failed_reiserfs_xattr_register_handlers:
2148 reiserfs_proc_unregister_global("version");
2149 reiserfs_proc_info_global_done();
2150 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 return ret;
2153}
2154
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002155static void __exit exit_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002157 reiserfs_xattr_unregister_handlers();
2158 reiserfs_proc_unregister_global("version");
2159 reiserfs_proc_info_global_done();
2160 unregister_filesystem(&reiserfs_fs_type);
2161 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
2164struct file_system_type reiserfs_fs_type = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002165 .owner = THIS_MODULE,
2166 .name = "reiserfs",
2167 .get_sb = get_super_block,
David Howellsedc666e2006-10-11 01:22:14 -07002168 .kill_sb = reiserfs_kill_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002169 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170};
2171
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002172MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2173MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2174MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002176module_init(init_reiserfs_fs);
2177module_exit(exit_reiserfs_fs);