blob: 879e54d35c2d49400a48b3d748a69c92934acb7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 *
4 * Trivial changes by Alan Cox to add the LFS fixes
5 *
6 * Trivial Changes:
7 * Rights granted to Hans Reiser to redistribute under other terms providing
8 * he accepts all liability including but not limited to patent, fitness
9 * for purpose, and direct or indirect claims arising from failure to perform.
10 *
11 * NO WARRANTY
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/vmalloc.h>
16#include <linux/time.h>
17#include <asm/uaccess.h>
18#include <linux/reiserfs_fs.h>
19#include <linux/reiserfs_acl.h>
20#include <linux/reiserfs_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
22#include <linux/blkdev.h>
23#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070024#include <linux/exportfs.h>
Jan Kara74abb982008-07-25 01:46:51 -070025#include <linux/quotaops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/vfs.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080027#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mount.h>
29#include <linux/namei.h>
30#include <linux/quotaops.h>
31
32struct file_system_type reiserfs_fs_type;
33
34static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
35static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
36static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
37
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070038int is_reiserfs_3_5(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070040 return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
41 strlen(reiserfs_3_5_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070044int is_reiserfs_3_6(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070046 return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
47 strlen(reiserfs_3_6_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070050int is_reiserfs_jr(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070052 return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
53 strlen(reiserfs_jr_magic_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070056static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070058 return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
59 is_reiserfs_jr(rs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070062static int reiserfs_remount(struct super_block *s, int *flags, char *data);
David Howells726c3342006-06-23 02:02:58 -070063static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065static int reiserfs_sync_fs(struct super_block *s, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067 if (!(s->s_flags & MS_RDONLY)) {
68 struct reiserfs_transaction_handle th;
69 reiserfs_write_lock(s);
70 if (!journal_begin(&th, s, 1))
71 if (!journal_end_sync(&th, s, 1))
72 reiserfs_flush_old_commits(s);
73 s->s_dirt = 0; /* Even if it's not true.
74 * We'll loop forever in sync_supers otherwise */
75 reiserfs_write_unlock(s);
76 } else {
77 s->s_dirt = 0;
78 }
79 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static void reiserfs_write_super(struct super_block *s)
83{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 reiserfs_sync_fs(s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070087static void reiserfs_write_super_lockfs(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070089 struct reiserfs_transaction_handle th;
90 reiserfs_write_lock(s);
91 if (!(s->s_flags & MS_RDONLY)) {
92 int err = journal_begin(&th, s, 1);
93 if (err) {
94 reiserfs_block_writes(&th);
95 } else {
96 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
97 1);
98 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
99 reiserfs_block_writes(&th);
100 journal_end_sync(&th, s, 1);
101 }
102 }
103 s->s_dirt = 0;
104 reiserfs_write_unlock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700107static void reiserfs_unlockfs(struct super_block *s)
108{
109 reiserfs_allow_writes(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700112extern const struct in_core_key MAX_IN_CORE_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* this is used to delete "save link" when there are no items of a
115 file it points to. It can either happen if unlink is completed but
116 "save unlink" removal, or if file has both unlink and truncate
117 pending and as unlink completes first (because key of "save link"
118 protecting unlink is bigger that a key lf "save link" which
119 protects truncate), so there left no items to make truncate
120 completion on */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700121static int remove_save_link_only(struct super_block *s,
122 struct reiserfs_key *key, int oid_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700124 struct reiserfs_transaction_handle th;
125 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700127 /* we are going to do one balancing */
128 err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
129 if (err)
130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700132 reiserfs_delete_solid_item(&th, NULL, key);
133 if (oid_free)
134 /* removals are protected by direct items */
135 reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));
136
137 return journal_end(&th, s, JOURNAL_PER_BALANCE_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#ifdef CONFIG_QUOTA
141static int reiserfs_quota_on_mount(struct super_block *, int);
142#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* look for uncompleted unlinks and truncates and complete them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145static int finish_unfinished(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 INITIALIZE_PATH(path);
148 struct cpu_key max_cpu_key, obj_key;
Lepton Wufb46f342007-10-16 23:30:04 -0700149 struct reiserfs_key save_link_key, last_inode_key;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700150 int retval = 0;
151 struct item_head *ih;
152 struct buffer_head *bh;
153 int item_pos;
154 char *item;
155 int done;
156 struct inode *inode;
157 int truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 int i;
160 int ms_active_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162
163 /* compose key to look for "save" links */
164 max_cpu_key.version = KEY_FORMAT_3_5;
165 max_cpu_key.on_disk_key.k_dir_id = ~0U;
166 max_cpu_key.on_disk_key.k_objectid = ~0U;
167 set_cpu_key_k_offset(&max_cpu_key, ~0U);
168 max_cpu_key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Lepton Wufb46f342007-10-16 23:30:04 -0700170 memset(&last_inode_key, 0, sizeof(last_inode_key));
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173 /* Needed for iput() to work correctly and not trash data */
174 if (s->s_flags & MS_ACTIVE) {
175 ms_active_set = 0;
176 } else {
177 ms_active_set = 1;
178 s->s_flags |= MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180 /* Turn on quotas so that they are updated correctly */
181 for (i = 0; i < MAXQUOTAS; i++) {
182 if (REISERFS_SB(s)->s_qf_names[i]) {
183 int ret = reiserfs_quota_on_mount(s, i);
184 if (ret < 0)
185 reiserfs_warning(s,
Jan Kara45065672008-07-25 01:46:37 -0700186 "reiserfs: cannot turn on journaled quota: error %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 ret);
188 }
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700192 done = 0;
193 REISERFS_SB(s)->s_is_unlinked_ok = 1;
194 while (!retval) {
195 retval = search_item(s, &max_cpu_key, &path);
196 if (retval != ITEM_NOT_FOUND) {
197 reiserfs_warning(s,
198 "vs-2140: finish_unfinished: search_by_key returned %d",
199 retval);
200 break;
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700203 bh = get_last_bh(&path);
204 item_pos = get_item_pos(&path);
205 if (item_pos != B_NR_ITEMS(bh)) {
206 reiserfs_warning(s,
207 "vs-2060: finish_unfinished: wrong position found");
208 break;
209 }
210 item_pos--;
211 ih = B_N_PITEM_HEAD(bh, item_pos);
212
213 if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
214 /* there are no "save" links anymore */
215 break;
216
217 save_link_key = ih->ih_key;
218 if (is_indirect_le_ih(ih))
219 truncate = 1;
220 else
221 truncate = 0;
222
223 /* reiserfs_iget needs k_dirid and k_objectid only */
224 item = B_I_PITEM(bh, ih);
225 obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
226 obj_key.on_disk_key.k_objectid =
227 le32_to_cpu(ih->ih_key.k_objectid);
228 obj_key.on_disk_key.k_offset = 0;
229 obj_key.on_disk_key.k_type = 0;
230
231 pathrelse(&path);
232
233 inode = reiserfs_iget(s, &obj_key);
234 if (!inode) {
235 /* the unlink almost completed, it just did not manage to remove
236 "save" link and release objectid */
237 reiserfs_warning(s,
238 "vs-2180: finish_unfinished: iget failed for %K",
239 &obj_key);
240 retval = remove_save_link_only(s, &save_link_key, 1);
241 continue;
242 }
243
244 if (!truncate && inode->i_nlink) {
245 /* file is not unlinked */
246 reiserfs_warning(s,
247 "vs-2185: finish_unfinished: file %K is not unlinked",
248 &obj_key);
249 retval = remove_save_link_only(s, &save_link_key, 0);
250 continue;
251 }
252 DQUOT_INIT(inode);
253
254 if (truncate && S_ISDIR(inode->i_mode)) {
255 /* We got a truncate request for a dir which is impossible.
256 The only imaginable way is to execute unfinished truncate request
257 then boot into old kernel, remove the file and create dir with
258 the same key. */
259 reiserfs_warning(s,
260 "green-2101: impossible truncate on a directory %k. Please report",
261 INODE_PKEY(inode));
262 retval = remove_save_link_only(s, &save_link_key, 0);
263 truncate = 0;
264 iput(inode);
265 continue;
266 }
267
268 if (truncate) {
269 REISERFS_I(inode)->i_flags |=
270 i_link_saved_truncate_mask;
271 /* not completed truncate found. New size was committed together
272 with "save" link */
273 reiserfs_info(s, "Truncating %k to %Ld ..",
274 INODE_PKEY(inode), inode->i_size);
275 reiserfs_truncate_file(inode,
276 0
277 /*don't update modification time */
278 );
279 retval = remove_save_link(inode, truncate);
280 } else {
281 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
282 /* not completed unlink (rmdir) found */
283 reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
Lepton Wufb46f342007-10-16 23:30:04 -0700284 if (memcmp(&last_inode_key, INODE_PKEY(inode),
285 sizeof(last_inode_key))){
286 last_inode_key = *INODE_PKEY(inode);
287 /* removal gets completed in iput */
288 retval = 0;
289 } else {
290 reiserfs_warning(s, "Dead loop in "
291 "finish_unfinished detected, "
292 "just remove save link\n");
293 retval = remove_save_link_only(s,
294 &save_link_key, 0);
295 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700296 }
297
298 iput(inode);
299 printk("done\n");
300 done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700302 REISERFS_SB(s)->s_is_unlinked_ok = 0;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700305 /* Turn quotas off */
306 for (i = 0; i < MAXQUOTAS; i++) {
307 if (sb_dqopt(s)->files[i])
Jan Kara1b445a92008-04-28 02:14:35 -0700308 vfs_quota_off(s, i, 0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700309 }
310 if (ms_active_set)
311 /* Restore the flag back */
312 s->s_flags &= ~MS_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 pathrelse(&path);
315 if (done)
316 reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
317 "Completed\n", done);
318 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/* to protect file being unlinked from getting lost we "safe" link files
322 being unlinked. This link will be deleted in the same transaction with last
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200323 item of file. mounting the filesystem we scan all these links and remove
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 files which almost got lost */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700325void add_save_link(struct reiserfs_transaction_handle *th,
326 struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700328 INITIALIZE_PATH(path);
329 int retval;
330 struct cpu_key key;
331 struct item_head ih;
332 __le32 link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700334 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700336 /* file can only get one "save link" of each kind */
337 RFALSE(truncate &&
338 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
339 "saved link already exists for truncated inode %lx",
340 (long)inode->i_ino);
341 RFALSE(!truncate &&
342 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
343 "saved link already exists for unlinked inode %lx",
344 (long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700346 /* setup key of "save" link */
347 key.version = KEY_FORMAT_3_5;
348 key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
349 key.on_disk_key.k_objectid = inode->i_ino;
350 if (!truncate) {
351 /* unlink, rmdir, rename */
352 set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
353 set_cpu_key_k_type(&key, TYPE_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700355 /* item head of "safe" link */
356 make_le_item_head(&ih, &key, key.version,
357 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
358 4 /*length */ , 0xffff /*free space */ );
359 } else {
360 /* truncate */
361 if (S_ISDIR(inode->i_mode))
362 reiserfs_warning(inode->i_sb,
363 "green-2102: Adding a truncate savelink for a directory %k! Please report",
364 INODE_PKEY(inode));
365 set_cpu_key_k_offset(&key, 1);
366 set_cpu_key_k_type(&key, TYPE_INDIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700368 /* item head of "safe" link */
369 make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
370 4 /*length */ , 0 /*free space */ );
371 }
372 key.key_length = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374 /* look for its place in the tree */
375 retval = search_item(inode->i_sb, &key, &path);
376 if (retval != ITEM_NOT_FOUND) {
377 if (retval != -ENOSPC)
378 reiserfs_warning(inode->i_sb, "vs-2100: add_save_link:"
379 "search_by_key (%K) returned %d", &key,
380 retval);
381 pathrelse(&path);
382 return;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 /* body of "save" link */
386 link = INODE_PKEY(inode)->k_dir_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 /* put "save" link inot tree, don't charge quota to anyone */
389 retval =
390 reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
391 if (retval) {
392 if (retval != -ENOSPC)
393 reiserfs_warning(inode->i_sb,
394 "vs-2120: add_save_link: insert_item returned %d",
395 retval);
396 } else {
397 if (truncate)
398 REISERFS_I(inode)->i_flags |=
399 i_link_saved_truncate_mask;
400 else
401 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/* this opens transaction unlike add_save_link */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700406int remove_save_link(struct inode *inode, int truncate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700408 struct reiserfs_transaction_handle th;
409 struct reiserfs_key key;
410 int err;
411
412 /* we are going to do one balancing only */
413 err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
414 if (err)
415 return err;
416
417 /* setup key of "save" link */
418 key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
419 key.k_objectid = INODE_PKEY(inode)->k_objectid;
420 if (!truncate) {
421 /* unlink, rmdir, rename */
422 set_le_key_k_offset(KEY_FORMAT_3_5, &key,
423 1 + inode->i_sb->s_blocksize);
424 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
425 } else {
426 /* truncate */
427 set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
428 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
429 }
430
431 if ((truncate &&
432 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
433 (!truncate &&
434 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
435 /* don't take quota bytes from anywhere */
436 reiserfs_delete_solid_item(&th, NULL, &key);
437 if (!truncate) {
438 reiserfs_release_objectid(&th, inode->i_ino);
439 REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
440 } else
441 REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
442
443 return journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
David Howellsedc666e2006-10-11 01:22:14 -0700446static void reiserfs_kill_sb(struct super_block *s)
447{
448 if (REISERFS_SB(s)) {
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -0700449#ifdef CONFIG_REISERFS_FS_XATTR
David Howellsedc666e2006-10-11 01:22:14 -0700450 if (REISERFS_SB(s)->xattr_root) {
451 d_invalidate(REISERFS_SB(s)->xattr_root);
452 dput(REISERFS_SB(s)->xattr_root);
453 REISERFS_SB(s)->xattr_root = NULL;
454 }
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -0700455#endif
David Howellsedc666e2006-10-11 01:22:14 -0700456 if (REISERFS_SB(s)->priv_root) {
457 d_invalidate(REISERFS_SB(s)->priv_root);
458 dput(REISERFS_SB(s)->priv_root);
459 REISERFS_SB(s)->priv_root = NULL;
460 }
461 }
462
463 kill_block_super(s);
464}
465
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700466static void reiserfs_put_super(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700468 struct reiserfs_transaction_handle th;
469 th.t_trans_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471 /* change file system state to current state if it was mounted with read-write permissions */
472 if (!(s->s_flags & MS_RDONLY)) {
473 if (!journal_begin(&th, s, 10)) {
474 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
475 1);
476 set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
477 REISERFS_SB(s)->s_mount_state);
478 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
479 }
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700482 /* note, journal_release checks for readonly mount, and can decide not
483 ** to do a journal_end
484 */
485 journal_release(&th, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Jeff Mahoney50652272006-09-30 23:28:44 -0700487 reiserfs_free_bitmap_cache(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700489 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700491 print_statistics(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700493 if (REISERFS_SB(s)->reserved_blocks != 0) {
494 reiserfs_warning(s,
495 "green-2005: reiserfs_put_super: reserved blocks left %d",
496 REISERFS_SB(s)->reserved_blocks);
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700499 reiserfs_proc_info_done(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700501 kfree(s->s_fs_info);
502 s->s_fs_info = NULL;
503
504 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Christoph Lametere18b8902006-12-06 20:33:20 -0800507static struct kmem_cache *reiserfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509static struct inode *reiserfs_alloc_inode(struct super_block *sb)
510{
511 struct reiserfs_inode_info *ei;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700512 ei = (struct reiserfs_inode_info *)
Christoph Lametere94b1762006-12-06 20:33:17 -0800513 kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!ei)
515 return NULL;
516 return &ei->vfs_inode;
517}
518
519static void reiserfs_destroy_inode(struct inode *inode)
520{
521 kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
522}
523
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700524static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700526 struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Christoph Lametera35afb82007-05-16 22:10:57 -0700528 INIT_LIST_HEAD(&ei->i_prealloc_list);
529 inode_init_once(&ei->vfs_inode);
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700530#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Christoph Lametera35afb82007-05-16 22:10:57 -0700531 ei->i_acl_access = NULL;
532 ei->i_acl_default = NULL;
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700533#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536static int init_inodecache(void)
537{
538 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700539 sizeof(struct
540 reiserfs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800541 0, (SLAB_RECLAIM_ACCOUNT|
542 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900543 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (reiserfs_inode_cachep == NULL)
545 return -ENOMEM;
546 return 0;
547}
548
549static void destroy_inodecache(void)
550{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700551 kmem_cache_destroy(reiserfs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/* we don't mark inodes dirty, we just log them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700555static void reiserfs_dirty_inode(struct inode *inode)
556{
557 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700559 int err = 0;
560 if (inode->i_sb->s_flags & MS_RDONLY) {
561 reiserfs_warning(inode->i_sb,
562 "clm-6006: writing inode %lu on readonly FS",
563 inode->i_ino);
564 return;
565 }
566 reiserfs_write_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700568 /* this is really only used for atime updates, so they don't have
569 ** to be included in O_SYNC or fsync
570 */
571 err = journal_begin(&th, inode->i_sb, 1);
572 if (err) {
573 reiserfs_write_unlock(inode->i_sb);
574 return;
575 }
576 reiserfs_update_sd(&th, inode);
577 journal_end(&th, inode->i_sb, 1);
578 reiserfs_write_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700581#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700582static void reiserfs_clear_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700584 struct posix_acl *acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700586 acl = REISERFS_I(inode)->i_acl_access;
587 if (acl && !IS_ERR(acl))
588 posix_acl_release(acl);
589 REISERFS_I(inode)->i_acl_access = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700591 acl = REISERFS_I(inode)->i_acl_default;
592 if (acl && !IS_ERR(acl))
593 posix_acl_release(acl);
594 REISERFS_I(inode)->i_acl_default = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
Alexey Dobriyancfe14672006-09-29 02:00:00 -0700596#else
597#define reiserfs_clear_inode NULL
598#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700601static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
602 size_t, loff_t);
603static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
604 loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605#endif
606
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800607static const struct super_operations reiserfs_sops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700608 .alloc_inode = reiserfs_alloc_inode,
609 .destroy_inode = reiserfs_destroy_inode,
610 .write_inode = reiserfs_write_inode,
611 .dirty_inode = reiserfs_dirty_inode,
612 .delete_inode = reiserfs_delete_inode,
613 .clear_inode = reiserfs_clear_inode,
614 .put_super = reiserfs_put_super,
615 .write_super = reiserfs_write_super,
616 .sync_fs = reiserfs_sync_fs,
617 .write_super_lockfs = reiserfs_write_super_lockfs,
618 .unlockfs = reiserfs_unlockfs,
619 .statfs = reiserfs_statfs,
620 .remount_fs = reiserfs_remount,
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -0800621 .show_options = generic_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700623 .quota_read = reiserfs_quota_read,
624 .quota_write = reiserfs_quota_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625#endif
626};
627
628#ifdef CONFIG_QUOTA
629#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
630
631static int reiserfs_dquot_initialize(struct inode *, int);
632static int reiserfs_dquot_drop(struct inode *);
633static int reiserfs_write_dquot(struct dquot *);
634static int reiserfs_acquire_dquot(struct dquot *);
635static int reiserfs_release_dquot(struct dquot *);
636static int reiserfs_mark_dquot_dirty(struct dquot *);
637static int reiserfs_write_info(struct super_block *, int);
Jan Kara1b445a92008-04-28 02:14:35 -0700638static int reiserfs_quota_on(struct super_block *, int, int, char *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700640static struct dquot_operations reiserfs_quota_operations = {
641 .initialize = reiserfs_dquot_initialize,
642 .drop = reiserfs_dquot_drop,
643 .alloc_space = dquot_alloc_space,
644 .alloc_inode = dquot_alloc_inode,
645 .free_space = dquot_free_space,
646 .free_inode = dquot_free_inode,
647 .transfer = dquot_transfer,
648 .write_dquot = reiserfs_write_dquot,
649 .acquire_dquot = reiserfs_acquire_dquot,
650 .release_dquot = reiserfs_release_dquot,
651 .mark_dirty = reiserfs_mark_dquot_dirty,
652 .write_info = reiserfs_write_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653};
654
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700655static struct quotactl_ops reiserfs_qctl_operations = {
656 .quota_on = reiserfs_quota_on,
657 .quota_off = vfs_quota_off,
658 .quota_sync = vfs_quota_sync,
659 .get_info = vfs_get_dqinfo,
660 .set_info = vfs_set_dqinfo,
661 .get_dqblk = vfs_get_dqblk,
662 .set_dqblk = vfs_set_dqblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663};
664#endif
665
Christoph Hellwig39655162007-10-21 16:42:17 -0700666static const struct export_operations reiserfs_export_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700667 .encode_fh = reiserfs_encode_fh,
Christoph Hellwigbe55caf2007-10-21 16:42:13 -0700668 .fh_to_dentry = reiserfs_fh_to_dentry,
669 .fh_to_parent = reiserfs_fh_to_parent,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700670 .get_parent = reiserfs_get_parent,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700671};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/* this struct is used in reiserfs_getopt () for containing the value for those
674 mount options that have values rather than being toggles. */
675typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700676 char *value;
677 int setmask; /* bitmask which is to set on mount_options bitmask when this
678 value is found, 0 is no bits are to be changed. */
679 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
680 value is found, 0 is no bits are to be changed. This is
681 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682} arg_desc_t;
683
684/* Set this bit in arg_required to allow empty arguments */
685#define REISERFS_OPT_ALLOWEMPTY 31
686
687/* this struct is used in reiserfs_getopt() for describing the set of reiserfs
688 mount options */
689typedef struct {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700690 char *option_name;
691 int arg_required; /* 0 if argument is not required, not 0 otherwise */
692 const arg_desc_t *values; /* list of values accepted by an option */
693 int setmask; /* bitmask which is to set on mount_options bitmask when this
694 value is found, 0 is no bits are to be changed. */
695 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
696 value is found, 0 is no bits are to be changed. This is
697 applied BEFORE setmask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698} opt_desc_t;
699
700/* possible values for -o data= */
701static const arg_desc_t logging_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700702 {"ordered", 1 << REISERFS_DATA_ORDERED,
703 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
704 {"journal", 1 << REISERFS_DATA_LOG,
705 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
706 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
707 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800708 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709};
710
711/* possible values for -o barrier= */
712static const arg_desc_t barrier_mode[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700713 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
714 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800715 {.value = NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716};
717
718/* possible values for "-o block-allocator=" and bits which are to be set in
719 s_mount_opt of reiserfs specific part of in-core super block */
720static const arg_desc_t balloc[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700721 {"noborder", 1 << REISERFS_NO_BORDER, 0},
722 {"border", 0, 1 << REISERFS_NO_BORDER},
723 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
724 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
725 {"test4", 1 << REISERFS_TEST4, 0},
726 {"notest4", 0, 1 << REISERFS_TEST4},
727 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728};
729
730static const arg_desc_t tails[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700731 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
732 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
733 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
734 {NULL, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
737static const arg_desc_t error_actions[] = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700738 {"panic", 1 << REISERFS_ERROR_PANIC,
739 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
740 {"ro-remount", 1 << REISERFS_ERROR_RO,
741 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742#ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 {"continue", 1 << REISERFS_ERROR_CONTINUE,
744 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700746 {NULL, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747};
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/* proceed only one option from a list *cur - string containing of mount options
750 opts - array of options which are accepted
751 opt_arg - if option is found and requires an argument and if it is specifed
752 in the input - pointer to the argument is stored here
753 bit_flags - if option requires to set a certain bit - it is set here
754 return -1 if unknown option is found, opt->arg_required otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700755static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
756 char **opt_arg, unsigned long *bit_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700758 char *p;
759 /* foo=bar,
760 ^ ^ ^
761 | | +-- option_end
762 | +-- arg_start
763 +-- option_start
764 */
765 const opt_desc_t *opt;
766 const arg_desc_t *arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768 p = *cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 /* assume argument cannot contain commas */
771 *cur = strchr(p, ',');
772 if (*cur) {
773 *(*cur) = '\0';
774 (*cur)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700777 if (!strncmp(p, "alloc=", 6)) {
778 /* Ugly special case, probably we should redo options parser so that
779 it can understand several arguments for some options, also so that
780 it can fill several bitfields with option values. */
781 if (reiserfs_parse_alloc_options(s, p + 6)) {
782 return -1;
783 } else {
784 return 0;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700787
788 /* for every option in the list */
789 for (opt = opts; opt->option_name; opt++) {
790 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
791 if (bit_flags) {
792 if (opt->clrmask ==
793 (1 << REISERFS_UNSUPPORTED_OPT))
794 reiserfs_warning(s, "%s not supported.",
795 p);
796 else
797 *bit_flags &= ~opt->clrmask;
798 if (opt->setmask ==
799 (1 << REISERFS_UNSUPPORTED_OPT))
800 reiserfs_warning(s, "%s not supported.",
801 p);
802 else
803 *bit_flags |= opt->setmask;
804 }
805 break;
806 }
807 }
808 if (!opt->option_name) {
809 reiserfs_warning(s, "unknown mount option \"%s\"", p);
810 return -1;
811 }
812
813 p += strlen(opt->option_name);
814 switch (*p) {
815 case '=':
816 if (!opt->arg_required) {
817 reiserfs_warning(s,
818 "the option \"%s\" does not require an argument",
819 opt->option_name);
820 return -1;
821 }
822 break;
823
824 case 0:
825 if (opt->arg_required) {
826 reiserfs_warning(s,
827 "the option \"%s\" requires an argument",
828 opt->option_name);
829 return -1;
830 }
831 break;
832 default:
833 reiserfs_warning(s, "head of option \"%s\" is only correct",
834 opt->option_name);
835 return -1;
836 }
837
838 /* move to the argument, or to next option if argument is not required */
839 p++;
840
841 if (opt->arg_required
842 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
843 && !strlen(p)) {
844 /* this catches "option=," if not allowed */
845 reiserfs_warning(s, "empty argument for \"%s\"",
846 opt->option_name);
847 return -1;
848 }
849
850 if (!opt->values) {
851 /* *=NULLopt_arg contains pointer to argument */
852 *opt_arg = p;
853 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
854 }
855
856 /* values possible for this option are listed in opt->values */
857 for (arg = opt->values; arg->value; arg++) {
858 if (!strcmp(p, arg->value)) {
859 if (bit_flags) {
860 *bit_flags &= ~arg->clrmask;
861 *bit_flags |= arg->setmask;
862 }
863 return opt->arg_required;
864 }
865 }
866
867 reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
868 opt->option_name);
869 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872/* returns 0 if something is wrong in option string, 1 - otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700873static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
874 unsigned long *mount_options,
875 /* after the parsing phase, contains the
876 collection of bitflags defining what
877 mount options were selected. */
878 unsigned long *blocks, /* strtol-ed from NNN of resize=NNN */
879 char **jdev_name,
Jan Kara00b44192008-07-25 01:46:38 -0700880 unsigned int *commit_max_age,
881 char **qf_names,
882 unsigned int *qfmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700884 int c;
885 char *arg = NULL;
886 char *pos;
887 opt_desc_t opts[] = {
888 /* Compatibility stuff, so that -o notail for old setups still work */
889 {"tails",.arg_required = 't',.values = tails},
890 {"notail",.clrmask =
891 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
892 {"conv",.setmask = 1 << REISERFS_CONVERT},
893 {"attrs",.setmask = 1 << REISERFS_ATTRS},
894 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700896 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
897 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700899 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
900 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#endif
902#ifdef CONFIG_REISERFS_FS_POSIX_ACL
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700903 {"acl",.setmask = 1 << REISERFS_POSIXACL},
904 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#else
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700906 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
907 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908#endif
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800909 {.option_name = "nolog"},
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700910 {"replayonly",.setmask = 1 << REPLAYONLY},
911 {"block-allocator",.arg_required = 'a',.values = balloc},
912 {"data",.arg_required = 'd',.values = logging_mode},
913 {"barrier",.arg_required = 'b',.values = barrier_mode},
914 {"resize",.arg_required = 'r',.values = NULL},
915 {"jdev",.arg_required = 'j',.values = NULL},
916 {"nolargeio",.arg_required = 'w',.values = NULL},
917 {"commit",.arg_required = 'c',.values = NULL},
918 {"usrquota",.setmask = 1 << REISERFS_QUOTA},
919 {"grpquota",.setmask = 1 << REISERFS_QUOTA},
920 {"noquota",.clrmask = 1 << REISERFS_QUOTA},
921 {"errors",.arg_required = 'e',.values = error_actions},
922 {"usrjquota",.arg_required =
923 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
924 {"grpjquota",.arg_required =
925 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
926 {"jqfmt",.arg_required = 'f',.values = NULL},
Vladimir V. Savelievcd02b962006-03-25 03:07:15 -0800927 {.option_name = NULL}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700928 };
929
930 *blocks = 0;
931 if (!options || !*options)
932 /* use default configuration: create tails, journaling on, no
933 conversion to newest format */
934 return 1;
935
936 for (pos = options; pos;) {
937 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
938 if (c == -1)
939 /* wrong option is given */
940 return 0;
941
942 if (c == 'r') {
943 char *p;
944
945 p = NULL;
946 /* "resize=NNN" or "resize=auto" */
947
948 if (!strcmp(arg, "auto")) {
949 /* From JFS code, to auto-get the size. */
950 *blocks =
951 s->s_bdev->bd_inode->i_size >> s->
952 s_blocksize_bits;
953 } else {
954 *blocks = simple_strtoul(arg, &p, 0);
955 if (*p != '\0') {
956 /* NNN does not look like a number */
957 reiserfs_warning(s,
958 "reiserfs_parse_options: bad value %s",
959 arg);
960 return 0;
961 }
962 }
963 }
964
965 if (c == 'c') {
966 char *p = NULL;
967 unsigned long val = simple_strtoul(arg, &p, 0);
968 /* commit=NNN (time in seconds) */
969 if (*p != '\0' || val >= (unsigned int)-1) {
970 reiserfs_warning(s,
971 "reiserfs_parse_options: bad value %s",
972 arg);
973 return 0;
974 }
975 *commit_max_age = (unsigned int)val;
976 }
977
978 if (c == 'w') {
Adrian Bunk36b756f2006-09-27 01:50:50 -0700979 reiserfs_warning(s, "reiserfs: nolargeio option is no longer supported");
980 return 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700981 }
982
983 if (c == 'j') {
984 if (arg && *arg && jdev_name) {
985 if (*jdev_name) { //Hm, already assigned?
986 reiserfs_warning(s,
987 "reiserfs_parse_options: journal device was already specified to be %s",
988 *jdev_name);
989 return 0;
990 }
991 *jdev_name = arg;
992 }
993 }
994#ifdef CONFIG_QUOTA
995 if (c == 'u' || c == 'g') {
996 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
997
Jan Kara00b44192008-07-25 01:46:38 -0700998 if ((sb_any_quota_enabled(s) ||
999 sb_any_quota_suspended(s)) &&
1000 (!*arg != !REISERFS_SB(s)->s_qf_names[qtype])) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 reiserfs_warning(s,
Jan Kara45065672008-07-25 01:46:37 -07001002 "reiserfs_parse_options: cannot change journaled quota options when quota turned on.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001003 return 0;
1004 }
1005 if (*arg) { /* Some filename specified? */
1006 if (REISERFS_SB(s)->s_qf_names[qtype]
1007 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
1008 arg)) {
1009 reiserfs_warning(s,
1010 "reiserfs_parse_options: %s quota file already specified.",
1011 QTYPE2NAME(qtype));
1012 return 0;
1013 }
1014 if (strchr(arg, '/')) {
1015 reiserfs_warning(s,
1016 "reiserfs_parse_options: quotafile must be on filesystem root.");
1017 return 0;
1018 }
Jan Kara00b44192008-07-25 01:46:38 -07001019 qf_names[qtype] =
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001020 kmalloc(strlen(arg) + 1, GFP_KERNEL);
Jan Kara00b44192008-07-25 01:46:38 -07001021 if (!qf_names[qtype]) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001022 reiserfs_warning(s,
1023 "reiserfs_parse_options: not enough memory for storing quotafile name.");
1024 return 0;
1025 }
Jan Kara00b44192008-07-25 01:46:38 -07001026 strcpy(qf_names[qtype], arg);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001027 *mount_options |= 1 << REISERFS_QUOTA;
1028 } else {
Jan Kara00b44192008-07-25 01:46:38 -07001029 if (qf_names[qtype] !=
1030 REISERFS_SB(s)->s_qf_names[qtype])
1031 kfree(qf_names[qtype]);
1032 qf_names[qtype] = NULL;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001033 }
1034 }
1035 if (c == 'f') {
1036 if (!strcmp(arg, "vfsold"))
Jan Kara00b44192008-07-25 01:46:38 -07001037 *qfmt = QFMT_VFS_OLD;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001038 else if (!strcmp(arg, "vfsv0"))
Jan Kara00b44192008-07-25 01:46:38 -07001039 *qfmt = QFMT_VFS_V0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001040 else {
1041 reiserfs_warning(s,
1042 "reiserfs_parse_options: unknown quota format specified.");
1043 return 0;
1044 }
Jan Kara00b44192008-07-25 01:46:38 -07001045 if ((sb_any_quota_enabled(s) ||
1046 sb_any_quota_suspended(s)) &&
1047 *qfmt != REISERFS_SB(s)->s_jquota_fmt) {
1048 reiserfs_warning(s,
1049 "reiserfs_parse_options: cannot change journaled quota options when quota turned on.");
1050 return 0;
1051 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001052 }
1053#else
1054 if (c == 'u' || c == 'g' || c == 'f') {
1055 reiserfs_warning(s,
Jan Kara45065672008-07-25 01:46:37 -07001056 "reiserfs_parse_options: journaled quota options not supported.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001057 return 0;
1058 }
1059#endif
1060 }
1061
1062#ifdef CONFIG_QUOTA
Jan Kara00b44192008-07-25 01:46:38 -07001063 if (!REISERFS_SB(s)->s_jquota_fmt && !*qfmt
1064 && (qf_names[USRQUOTA] || qf_names[GRPQUOTA])) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001065 reiserfs_warning(s,
Jan Kara45065672008-07-25 01:46:37 -07001066 "reiserfs_parse_options: journaled quota format not specified.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001067 return 0;
1068 }
1069 /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
1070 if (!(*mount_options & (1 << REISERFS_QUOTA))
1071 && sb_any_quota_enabled(s)) {
1072 reiserfs_warning(s,
1073 "reiserfs_parse_options: quota options must be present when quota is turned on.");
1074 return 0;
1075 }
1076#endif
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001081static void switch_data_mode(struct super_block *s, unsigned long mode)
1082{
1083 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1084 (1 << REISERFS_DATA_ORDERED) |
1085 (1 << REISERFS_DATA_WRITEBACK));
1086 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1090{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1092 if (!reiserfs_data_log(s)) {
1093 switch_data_mode(s, REISERFS_DATA_LOG);
1094 reiserfs_info(s, "switching to journaled data mode\n");
1095 }
1096 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1097 if (!reiserfs_data_ordered(s)) {
1098 switch_data_mode(s, REISERFS_DATA_ORDERED);
1099 reiserfs_info(s, "switching to ordered data mode\n");
1100 }
1101 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1102 if (!reiserfs_data_writeback(s)) {
1103 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1104 reiserfs_info(s, "switching to writeback data mode\n");
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109static void handle_barrier_mode(struct super_block *s, unsigned long bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001111 int flush = (1 << REISERFS_BARRIER_FLUSH);
1112 int none = (1 << REISERFS_BARRIER_NONE);
1113 int all_barrier = flush | none;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001115 if (bits & all_barrier) {
1116 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1117 if (bits & flush) {
1118 REISERFS_SB(s)->s_mount_opt |= flush;
1119 printk("reiserfs: enabling write barrier flush mode\n");
1120 } else if (bits & none) {
1121 REISERFS_SB(s)->s_mount_opt |= none;
1122 printk("reiserfs: write barriers turned off\n");
1123 }
1124 }
1125}
1126
1127static void handle_attrs(struct super_block *s)
1128{
1129 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1130
1131 if (reiserfs_attrs(s)) {
1132 if (old_format_only(s)) {
1133 reiserfs_warning(s,
1134 "reiserfs: cannot support attributes on 3.5.x disk format");
1135 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return;
1137 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001138 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1139 reiserfs_warning(s,
1140 "reiserfs: cannot support attributes until flag is set in super-block");
1141 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 }
1144}
1145
Jan Kara00b44192008-07-25 01:46:38 -07001146#ifdef CONFIG_QUOTA
1147static void handle_quota_files(struct super_block *s, char **qf_names,
1148 unsigned int *qfmt)
1149{
1150 int i;
1151
1152 for (i = 0; i < MAXQUOTAS; i++) {
1153 if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1154 kfree(REISERFS_SB(s)->s_qf_names[i]);
1155 REISERFS_SB(s)->s_qf_names[i] = qf_names[i];
1156 }
1157 REISERFS_SB(s)->s_jquota_fmt = *qfmt;
1158}
1159#endif
1160
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001161static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001163 struct reiserfs_super_block *rs;
1164 struct reiserfs_transaction_handle th;
1165 unsigned long blocks;
1166 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1167 unsigned long safe_mask = 0;
1168 unsigned int commit_max_age = (unsigned int)-1;
1169 struct reiserfs_journal *journal = SB_JOURNAL(s);
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001170 char *new_opts = kstrdup(arg, GFP_KERNEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001171 int err;
Jan Kara00b44192008-07-25 01:46:38 -07001172 char *qf_names[MAXQUOTAS];
1173 unsigned int qfmt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001175 int i;
Jan Kara00b44192008-07-25 01:46:38 -07001176
1177 memcpy(qf_names, REISERFS_SB(s)->s_qf_names, sizeof(qf_names));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178#endif
1179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001180 rs = SB_DISK_SUPER_BLOCK(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001182 if (!reiserfs_parse_options
Jan Kara00b44192008-07-25 01:46:38 -07001183 (s, arg, &mount_options, &blocks, NULL, &commit_max_age,
1184 qf_names, &qfmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185#ifdef CONFIG_QUOTA
Jan Kara00b44192008-07-25 01:46:38 -07001186 for (i = 0; i < MAXQUOTAS; i++)
1187 if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1188 kfree(qf_names[i]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001189#endif
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001190 err = -EINVAL;
1191 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Jan Kara00b44192008-07-25 01:46:38 -07001193#ifdef CONFIG_QUOTA
1194 handle_quota_files(s, qf_names, &qfmt);
1195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001197 handle_attrs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001199 /* Add options that are safe here */
1200 safe_mask |= 1 << REISERFS_SMALLTAIL;
1201 safe_mask |= 1 << REISERFS_LARGETAIL;
1202 safe_mask |= 1 << REISERFS_NO_BORDER;
1203 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1204 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1205 safe_mask |= 1 << REISERFS_TEST4;
1206 safe_mask |= 1 << REISERFS_ATTRS;
1207 safe_mask |= 1 << REISERFS_XATTRS_USER;
1208 safe_mask |= 1 << REISERFS_POSIXACL;
1209 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1210 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1211 safe_mask |= 1 << REISERFS_ERROR_RO;
1212 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1213 safe_mask |= 1 << REISERFS_ERROR_PANIC;
1214 safe_mask |= 1 << REISERFS_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001216 /* Update the bitmask, taking care to keep
1217 * the bits we're not allowed to change here */
1218 REISERFS_SB(s)->s_mount_opt =
1219 (REISERFS_SB(s)->
1220 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001222 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1223 journal->j_max_commit_age = commit_max_age;
1224 journal->j_max_trans_age = commit_max_age;
1225 } else if (commit_max_age == 0) {
1226 /* 0 means restore defaults. */
1227 journal->j_max_commit_age = journal->j_default_max_commit_age;
1228 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001231 if (blocks) {
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001232 err = reiserfs_resize(s, blocks);
1233 if (err != 0)
1234 goto out_err;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001237 if (*mount_flags & MS_RDONLY) {
1238 reiserfs_xattr_init(s, *mount_flags);
1239 /* remount read-only */
1240 if (s->s_flags & MS_RDONLY)
1241 /* it is read-only already */
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001242 goto out_ok;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001243 /* try to remount file system with read-only permissions */
1244 if (sb_umount_state(rs) == REISERFS_VALID_FS
1245 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001246 goto out_ok;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001249 err = journal_begin(&th, s, 10);
1250 if (err)
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001251 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001253 /* Mounting a rw partition read-only. */
1254 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1255 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1256 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1257 } else {
1258 /* remount read-write */
1259 if (!(s->s_flags & MS_RDONLY)) {
1260 reiserfs_xattr_init(s, *mount_flags);
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001261 goto out_ok; /* We are read-write already */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001264 if (reiserfs_is_journal_aborted(journal)) {
1265 err = journal->j_errno;
1266 goto out_err;
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001269 handle_data_mode(s, mount_options);
1270 handle_barrier_mode(s, mount_options);
1271 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1272 s->s_flags &= ~MS_RDONLY; /* now it is safe to call journal_begin */
1273 err = journal_begin(&th, s, 10);
1274 if (err)
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001275 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001277 /* Mount a partition which is read-only, read-write */
1278 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1279 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1280 s->s_flags &= ~MS_RDONLY;
1281 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1282 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1283 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1284 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1285 }
1286 /* this will force a full flush of all journal lists */
1287 SB_JOURNAL(s)->j_must_wait = 1;
1288 err = journal_end(&th, s, 10);
1289 if (err)
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001290 goto out_err;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001291 s->s_dirt = 0;
1292
1293 if (!(*mount_flags & MS_RDONLY)) {
1294 finish_unfinished(s);
1295 reiserfs_xattr_init(s, *mount_flags);
1296 }
1297
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001298out_ok:
1299 kfree(s->s_options);
1300 s->s_options = new_opts;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001301 return 0;
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001302
1303out_err:
1304 kfree(new_opts);
1305 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001308static int read_super_block(struct super_block *s, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001310 struct buffer_head *bh;
1311 struct reiserfs_super_block *rs;
1312 int fs_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001314 bh = sb_bread(s, offset / s->s_blocksize);
1315 if (!bh) {
1316 reiserfs_warning(s, "sh-2006: read_super_block: "
1317 "bread failed (dev %s, block %lu, size %lu)",
1318 reiserfs_bdevname(s), offset / s->s_blocksize,
1319 s->s_blocksize);
1320 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001323 rs = (struct reiserfs_super_block *)bh->b_data;
1324 if (!is_any_reiserfs_magic_string(rs)) {
1325 brelse(bh);
1326 return 1;
1327 }
1328 //
1329 // ok, reiserfs signature (old or new) found in at the given offset
1330 //
1331 fs_blocksize = sb_blocksize(rs);
1332 brelse(bh);
1333 sb_set_blocksize(s, fs_blocksize);
1334
1335 bh = sb_bread(s, offset / s->s_blocksize);
1336 if (!bh) {
1337 reiserfs_warning(s, "sh-2007: read_super_block: "
1338 "bread failed (dev %s, block %lu, size %lu)\n",
1339 reiserfs_bdevname(s), offset / s->s_blocksize,
1340 s->s_blocksize);
1341 return 1;
1342 }
1343
1344 rs = (struct reiserfs_super_block *)bh->b_data;
1345 if (sb_blocksize(rs) != s->s_blocksize) {
1346 reiserfs_warning(s, "sh-2011: read_super_block: "
1347 "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1348 reiserfs_bdevname(s),
1349 (unsigned long long)bh->b_blocknr,
1350 s->s_blocksize);
1351 brelse(bh);
1352 return 1;
1353 }
1354
1355 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1356 brelse(bh);
1357 reiserfs_warning(s,
1358 "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1359 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1360 "get newer reiserfsprogs package");
1361 return 1;
1362 }
1363
1364 SB_BUFFER_WITH_SB(s) = bh;
1365 SB_DISK_SUPER_BLOCK(s) = rs;
1366
1367 if (is_reiserfs_jr(rs)) {
1368 /* magic is of non-standard journal filesystem, look at s_version to
1369 find which format is in use */
1370 if (sb_version(rs) == REISERFS_VERSION_2)
1371 reiserfs_warning(s,
1372 "read_super_block: found reiserfs format \"3.6\""
1373 " with non-standard journal");
1374 else if (sb_version(rs) == REISERFS_VERSION_1)
1375 reiserfs_warning(s,
1376 "read_super_block: found reiserfs format \"3.5\""
1377 " with non-standard journal");
1378 else {
1379 reiserfs_warning(s,
1380 "sh-2012: read_super_block: found unknown "
1381 "format \"%u\" of reiserfs with non-standard magic",
1382 sb_version(rs));
1383 return 1;
1384 }
1385 } else
1386 /* s_version of standard format may contain incorrect information,
1387 so we just look at the magic string */
1388 reiserfs_info(s,
1389 "found reiserfs format \"%s\" with standard journal\n",
1390 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1391
1392 s->s_op = &reiserfs_sops;
1393 s->s_export_op = &reiserfs_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394#ifdef CONFIG_QUOTA
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001395 s->s_qcop = &reiserfs_qctl_operations;
1396 s->dq_op = &reiserfs_quota_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397#endif
1398
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001399 /* new format is limited by the 32 bit wide i_blocks field, want to
1400 ** be one full block below that.
1401 */
1402 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406/* after journal replay, reread all bitmap and super blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001407static int reread_meta_blocks(struct super_block *s)
1408{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001409 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
1410 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1411 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1412 reiserfs_warning(s,
1413 "reread_meta_blocks, error reading the super");
1414 return 1;
1415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420/////////////////////////////////////////////////////
1421// hash detection stuff
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423// if root directory is empty - we set default - Yura's - hash and
1424// warn about it
1425// FIXME: we look for only one name in a directory. If tea and yura
1426// bith have the same value - we ask user to send report to the
1427// mailing list
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428static __u32 find_hash_out(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001430 int retval;
1431 struct inode *inode;
1432 struct cpu_key key;
1433 INITIALIZE_PATH(path);
1434 struct reiserfs_dir_entry de;
1435 __u32 hash = DEFAULT_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001437 inode = s->s_root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001439 do { // Some serious "goto"-hater was there ;)
1440 u32 teahash, r5hash, yurahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001442 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1443 retval = search_by_entry_key(s, &key, &path, &de);
1444 if (retval == IO_ERROR) {
1445 pathrelse(&path);
1446 return UNSET_HASH;
1447 }
1448 if (retval == NAME_NOT_FOUND)
1449 de.de_entry_num--;
1450 set_de_name_and_namelen(&de);
1451 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1452 /* allow override in this case */
1453 if (reiserfs_rupasov_hash(s)) {
1454 hash = YURA_HASH;
1455 }
1456 reiserfs_warning(s, "FS seems to be empty, autodetect "
1457 "is using the default hash");
1458 break;
1459 }
1460 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1461 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1462 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1463 if (((teahash == r5hash)
1464 &&
1465 (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1466 == r5hash)) || ((teahash == yurahash)
1467 && (yurahash ==
1468 GET_HASH_VALUE(deh_offset
1469 (&
1470 (de.
1471 de_deh[de.
1472 de_entry_num])))))
1473 || ((r5hash == yurahash)
1474 && (yurahash ==
1475 GET_HASH_VALUE(deh_offset
1476 (&(de.de_deh[de.de_entry_num])))))) {
1477 reiserfs_warning(s,
1478 "Unable to automatically detect hash function. "
1479 "Please mount with -o hash={tea,rupasov,r5}",
1480 reiserfs_bdevname(s));
1481 hash = UNSET_HASH;
1482 break;
1483 }
1484 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1485 yurahash)
1486 hash = YURA_HASH;
1487 else if (GET_HASH_VALUE
1488 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1489 hash = TEA_HASH;
1490 else if (GET_HASH_VALUE
1491 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1492 hash = R5_HASH;
1493 else {
1494 reiserfs_warning(s, "Unrecognised hash function");
1495 hash = UNSET_HASH;
1496 }
1497 } while (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001499 pathrelse(&path);
1500 return hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503// finds out which hash names are sorted with
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001504static int what_hash(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001506 __u32 code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001508 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001510 /* reiserfs_hash_detect() == true if any of the hash mount options
1511 ** were used. We must check them to make sure the user isn't
1512 ** using a bad hash value
1513 */
1514 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1515 code = find_hash_out(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001517 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1518 /* detection has found the hash, and we must check against the
1519 ** mount options
1520 */
1521 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1522 reiserfs_warning(s, "Error, %s hash detected, "
1523 "unable to force rupasov hash",
1524 reiserfs_hashname(code));
1525 code = UNSET_HASH;
1526 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1527 reiserfs_warning(s, "Error, %s hash detected, "
1528 "unable to force tea hash",
1529 reiserfs_hashname(code));
1530 code = UNSET_HASH;
1531 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1532 reiserfs_warning(s, "Error, %s hash detected, "
1533 "unable to force r5 hash",
1534 reiserfs_hashname(code));
1535 code = UNSET_HASH;
1536 }
1537 } else {
1538 /* find_hash_out was not called or could not determine the hash */
1539 if (reiserfs_rupasov_hash(s)) {
1540 code = YURA_HASH;
1541 } else if (reiserfs_tea_hash(s)) {
1542 code = TEA_HASH;
1543 } else if (reiserfs_r5_hash(s)) {
1544 code = R5_HASH;
1545 }
1546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001548 /* if we are mounted RW, and we have a new valid hash code, update
1549 ** the super
1550 */
1551 if (code != UNSET_HASH &&
1552 !(s->s_flags & MS_RDONLY) &&
1553 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1554 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1555 }
1556 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559// return pointer to appropriate function
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001560static hashf_t hash_function(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001562 switch (what_hash(s)) {
1563 case TEA_HASH:
1564 reiserfs_info(s, "Using tea hash to sort names\n");
1565 return keyed_hash;
1566 case YURA_HASH:
1567 reiserfs_info(s, "Using rupasov hash to sort names\n");
1568 return yura_hash;
1569 case R5_HASH:
1570 reiserfs_info(s, "Using r5 hash to sort names\n");
1571 return r5_hash;
1572 }
1573 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576// this is used to set up correct value for old partitions
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001577static int function2code(hashf_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001579 if (func == keyed_hash)
1580 return TEA_HASH;
1581 if (func == yura_hash)
1582 return YURA_HASH;
1583 if (func == r5_hash)
1584 return R5_HASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001586 BUG(); // should never happen
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
1591#define SWARN(silent, s, ...) \
1592 if (!(silent)) \
1593 reiserfs_warning (s, __VA_ARGS__)
1594
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001595static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001597 struct inode *root_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001598 struct reiserfs_transaction_handle th;
1599 int old_format = 0;
1600 unsigned long blocks;
1601 unsigned int commit_max_age = 0;
1602 int jinit_done = 0;
1603 struct reiserfs_iget_args args;
1604 struct reiserfs_super_block *rs;
1605 char *jdev_name;
1606 struct reiserfs_sb_info *sbi;
1607 int errval = -EINVAL;
Jan Kara00b44192008-07-25 01:46:38 -07001608 char *qf_names[MAXQUOTAS] = {};
1609 unsigned int qfmt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Miklos Szeredicdf6ccc2008-02-08 04:21:47 -08001611 save_mount_options(s, data);
1612
Yan Burman01afb212006-12-06 20:39:01 -08001613 sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001614 if (!sbi) {
1615 errval = -ENOMEM;
1616 goto error;
1617 }
1618 s->s_fs_info = sbi;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001619 /* Set default values for options: non-aggressive tails, RO on errors */
1620 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1621 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1622 /* no preallocation minimum, be smart in
1623 reiserfs_file_write instead */
1624 REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1625 /* Preallocate by 16 blocks (17-1) at once */
1626 REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001627#ifdef CONFIG_REISERFS_FS_XATTR
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001628 /* Initialize the rwsem for xattr dir */
1629 init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
Alexey Dobriyanfe08a9d2007-05-08 00:24:55 -07001630#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001631 /* setup default block allocator options */
1632 reiserfs_init_alloc_options(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001634 jdev_name = NULL;
1635 if (reiserfs_parse_options
1636 (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
Jan Kara00b44192008-07-25 01:46:38 -07001637 &commit_max_age, qf_names, &qfmt) == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001638 goto error;
1639 }
Jan Kara00b44192008-07-25 01:46:38 -07001640#ifdef CONFIG_QUOTA
1641 handle_quota_files(s, qf_names, &qfmt);
1642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001644 if (blocks) {
1645 SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1646 "for remount only");
1647 goto error;
1648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001650 /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1651 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1652 old_format = 1;
1653 /* try new format (64-th 1k block), which can contain reiserfs super block */
1654 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1655 SWARN(silent, s,
1656 "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
1657 reiserfs_bdevname(s));
1658 goto error;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001661 rs = SB_DISK_SUPER_BLOCK(s);
1662 /* Let's do basic sanity check to verify that underlying device is not
1663 smaller than the filesystem. If the check fails then abort and scream,
1664 because bad stuff will happen otherwise. */
1665 if (s->s_bdev && s->s_bdev->bd_inode
1666 && i_size_read(s->s_bdev->bd_inode) <
1667 sb_block_count(rs) * sb_blocksize(rs)) {
1668 SWARN(silent, s,
1669 "Filesystem on %s cannot be mounted because it is bigger than the device",
1670 reiserfs_bdevname(s));
1671 SWARN(silent, s,
1672 "You may need to run fsck or increase size of your LVM partition");
1673 SWARN(silent, s,
1674 "Or may be you forgot to reboot after fdisk when it told you to");
1675 goto error;
1676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001678 sbi->s_mount_state = SB_REISERFS_STATE(s);
1679 sbi->s_mount_state = REISERFS_VALID_FS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Jeff Mahoney6f010462006-09-30 23:28:43 -07001681 if ((errval = reiserfs_init_bitmap_cache(s))) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001682 SWARN(silent, s,
1683 "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1684 goto error;
1685 }
Jeff Mahoneyd2c89a42006-11-02 22:07:20 -08001686 errval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001688 SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
1689 SWARN(silent, s, "- it is slow mode for debugging.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690#endif
1691
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001692 /* make data=ordered the default */
1693 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1694 !reiserfs_data_writeback(s)) {
1695 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001698 if (reiserfs_data_log(s)) {
1699 reiserfs_info(s, "using journaled data mode\n");
1700 } else if (reiserfs_data_ordered(s)) {
1701 reiserfs_info(s, "using ordered data mode\n");
1702 } else {
1703 reiserfs_info(s, "using writeback data mode\n");
1704 }
1705 if (reiserfs_barrier_flush(s)) {
1706 printk("reiserfs: using flush barriers\n");
1707 }
1708 // set_device_ro(s->s_dev, 1) ;
1709 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1710 SWARN(silent, s,
1711 "sh-2022: reiserfs_fill_super: unable to initialize journal space");
1712 goto error;
1713 } else {
1714 jinit_done = 1; /* once this is set, journal_release must be called
1715 ** if we error out of the mount
1716 */
1717 }
1718 if (reread_meta_blocks(s)) {
1719 SWARN(silent, s,
1720 "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
1721 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001724 if (replay_only(s))
1725 goto error;
1726
1727 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1728 SWARN(silent, s,
1729 "clm-7000: Detected readonly device, marking FS readonly");
1730 s->s_flags |= MS_RDONLY;
1731 }
1732 args.objectid = REISERFS_ROOT_OBJECTID;
1733 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
1734 root_inode =
1735 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
1736 reiserfs_init_locked_inode, (void *)(&args));
1737 if (!root_inode) {
1738 SWARN(silent, s,
1739 "jmacd-10: reiserfs_fill_super: get root inode failed");
1740 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001743 if (root_inode->i_state & I_NEW) {
1744 reiserfs_read_locked_inode(root_inode, &args);
1745 unlock_new_inode(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001748 s->s_root = d_alloc_root(root_inode);
1749 if (!s->s_root) {
1750 iput(root_inode);
1751 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001753 // define and initialize hash function
1754 sbi->s_hash_function = hash_function(s);
1755 if (sbi->s_hash_function == NULL) {
1756 dput(s->s_root);
1757 s->s_root = NULL;
1758 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001760
1761 if (is_reiserfs_3_5(rs)
1762 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
1763 set_bit(REISERFS_3_5, &(sbi->s_properties));
Jeff Mahoneye1fabd32006-09-30 23:28:40 -07001764 else if (old_format)
1765 set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001766 else
1767 set_bit(REISERFS_3_6, &(sbi->s_properties));
1768
1769 if (!(s->s_flags & MS_RDONLY)) {
1770
1771 errval = journal_begin(&th, s, 1);
1772 if (errval) {
1773 dput(s->s_root);
1774 s->s_root = NULL;
1775 goto error;
1776 }
1777 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1778
1779 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1780 set_sb_fs_state(rs, 0);
1781
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001782 /* Clear out s_bmap_nr if it would wrap. We can handle this
1783 * case, but older revisions can't. This will cause the
1784 * file system to fail mount on those older implementations,
1785 * avoiding corruption. -jeffm */
1786 if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
1787 sb_bmap_nr(rs) != 0) {
1788 reiserfs_warning(s, "super-2030: This file system "
1789 "claims to use %u bitmap blocks in "
1790 "its super block, but requires %u. "
1791 "Clearing to zero.", sb_bmap_nr(rs),
1792 reiserfs_bmap_count(s));
1793
1794 set_sb_bmap_nr(rs, 0);
1795 }
1796
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001797 if (old_format_only(s)) {
1798 /* filesystem of format 3.5 either with standard or non-standard
1799 journal */
1800 if (convert_reiserfs(s)) {
1801 /* and -o conv is given */
1802 if (!silent)
1803 reiserfs_info(s,
1804 "converting 3.5 filesystem to the 3.6 format");
1805
1806 if (is_reiserfs_3_5(rs))
1807 /* put magic string of 3.6 format. 2.2 will not be able to
1808 mount this filesystem anymore */
1809 memcpy(rs->s_v1.s_magic,
1810 reiserfs_3_6_magic_string,
1811 sizeof
1812 (reiserfs_3_6_magic_string));
1813
1814 set_sb_version(rs, REISERFS_VERSION_2);
1815 reiserfs_convert_objectid_map_v1(s);
1816 set_bit(REISERFS_3_6, &(sbi->s_properties));
1817 clear_bit(REISERFS_3_5, &(sbi->s_properties));
1818 } else if (!silent) {
1819 reiserfs_info(s, "using 3.5.x disk format\n");
1820 }
1821 }
1822
1823 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1824 errval = journal_end(&th, s, 1);
1825 if (errval) {
1826 dput(s->s_root);
1827 s->s_root = NULL;
1828 goto error;
1829 }
1830
1831 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1832 dput(s->s_root);
1833 s->s_root = NULL;
1834 goto error;
1835 }
1836
1837 /* look for files which were to be removed in previous session */
1838 finish_unfinished(s);
1839 } else {
1840 if (old_format_only(s) && !silent) {
1841 reiserfs_info(s, "using 3.5.x disk format\n");
1842 }
1843
1844 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1845 dput(s->s_root);
1846 s->s_root = NULL;
1847 goto error;
1848 }
1849 }
1850 // mark hash in super block: it could be unset. overwrite should be ok
1851 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
1852
1853 handle_attrs(s);
1854
1855 reiserfs_proc_info_init(s);
1856
1857 init_waitqueue_head(&(sbi->s_wait));
1858 spin_lock_init(&sbi->bitmap_lock);
1859
1860 return (0);
1861
Jan Kara00b44192008-07-25 01:46:38 -07001862error:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001863 if (jinit_done) { /* kill the commit thread, free journal ram */
1864 journal_release_error(NULL, s);
1865 }
Jeff Mahoney50652272006-09-30 23:28:44 -07001866
1867 reiserfs_free_bitmap_cache(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001868 if (SB_BUFFER_WITH_SB(s))
1869 brelse(SB_BUFFER_WITH_SB(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870#ifdef CONFIG_QUOTA
Jeff Mahoney50652272006-09-30 23:28:44 -07001871 {
1872 int j;
Jan Kara00b44192008-07-25 01:46:38 -07001873 for (j = 0; j < MAXQUOTAS; j++)
1874 kfree(qf_names[j]);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876#endif
James Lamanna833d3042005-10-30 15:00:16 -08001877 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001879 s->s_fs_info = NULL;
1880 return errval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
David Howells726c3342006-06-23 02:02:58 -07001883static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
David Howells726c3342006-06-23 02:02:58 -07001885 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001886
1887 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
1888 buf->f_bfree = sb_free_blocks(rs);
1889 buf->f_bavail = buf->f_bfree;
1890 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
David Howells726c3342006-06-23 02:02:58 -07001891 buf->f_bsize = dentry->d_sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001892 /* changed to accommodate gcc folks. */
1893 buf->f_type = REISERFS_SUPER_MAGIC;
1894 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
1897#ifdef CONFIG_QUOTA
1898static int reiserfs_dquot_initialize(struct inode *inode, int type)
1899{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001900 struct reiserfs_transaction_handle th;
1901 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001903 /* We may create quota structure so we need to reserve enough blocks */
1904 reiserfs_write_lock(inode->i_sb);
1905 ret =
1906 journal_begin(&th, inode->i_sb,
1907 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1908 if (ret)
1909 goto out;
1910 ret = dquot_initialize(inode, type);
1911 err =
1912 journal_end(&th, inode->i_sb,
1913 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1914 if (!ret && err)
1915 ret = err;
1916 out:
1917 reiserfs_write_unlock(inode->i_sb);
1918 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
1921static int reiserfs_dquot_drop(struct inode *inode)
1922{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001923 struct reiserfs_transaction_handle th;
1924 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001926 /* We may delete quota structure so we need to reserve enough blocks */
1927 reiserfs_write_lock(inode->i_sb);
1928 ret =
1929 journal_begin(&th, inode->i_sb,
1930 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
Jan Karaa2fe5942008-04-28 02:16:23 -07001931 if (ret) {
1932 /*
1933 * We call dquot_drop() anyway to at least release references
1934 * to quota structures so that umount does not hang.
1935 */
1936 dquot_drop(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001937 goto out;
Jan Karaa2fe5942008-04-28 02:16:23 -07001938 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001939 ret = dquot_drop(inode);
1940 err =
1941 journal_end(&th, inode->i_sb,
1942 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1943 if (!ret && err)
1944 ret = err;
1945 out:
1946 reiserfs_write_unlock(inode->i_sb);
1947 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
1950static int reiserfs_write_dquot(struct dquot *dquot)
1951{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001952 struct reiserfs_transaction_handle th;
1953 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001955 reiserfs_write_lock(dquot->dq_sb);
1956 ret =
1957 journal_begin(&th, dquot->dq_sb,
1958 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1959 if (ret)
1960 goto out;
1961 ret = dquot_commit(dquot);
1962 err =
1963 journal_end(&th, dquot->dq_sb,
1964 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1965 if (!ret && err)
1966 ret = err;
1967 out:
1968 reiserfs_write_unlock(dquot->dq_sb);
1969 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972static int reiserfs_acquire_dquot(struct dquot *dquot)
1973{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001974 struct reiserfs_transaction_handle th;
1975 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001977 reiserfs_write_lock(dquot->dq_sb);
1978 ret =
1979 journal_begin(&th, dquot->dq_sb,
1980 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1981 if (ret)
1982 goto out;
1983 ret = dquot_acquire(dquot);
1984 err =
1985 journal_end(&th, dquot->dq_sb,
1986 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1987 if (!ret && err)
1988 ret = err;
1989 out:
1990 reiserfs_write_unlock(dquot->dq_sb);
1991 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994static int reiserfs_release_dquot(struct dquot *dquot)
1995{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001996 struct reiserfs_transaction_handle th;
1997 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001999 reiserfs_write_lock(dquot->dq_sb);
2000 ret =
2001 journal_begin(&th, dquot->dq_sb,
2002 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07002003 if (ret) {
2004 /* Release dquot anyway to avoid endless cycle in dqput() */
2005 dquot_release(dquot);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002006 goto out;
Jan Kara9c3013e2007-09-11 15:23:29 -07002007 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002008 ret = dquot_release(dquot);
2009 err =
2010 journal_end(&th, dquot->dq_sb,
2011 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2012 if (!ret && err)
2013 ret = err;
2014 out:
2015 reiserfs_write_unlock(dquot->dq_sb);
2016 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
2019static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
2020{
Jan Kara45065672008-07-25 01:46:37 -07002021 /* Are we journaling quotas? */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002022 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2023 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2024 dquot_mark_dquot_dirty(dquot);
2025 return reiserfs_write_dquot(dquot);
2026 } else
2027 return dquot_mark_dquot_dirty(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
2030static int reiserfs_write_info(struct super_block *sb, int type)
2031{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002032 struct reiserfs_transaction_handle th;
2033 int ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002035 /* Data block + inode block */
2036 reiserfs_write_lock(sb);
2037 ret = journal_begin(&th, sb, 2);
2038 if (ret)
2039 goto out;
2040 ret = dquot_commit_info(sb, type);
2041 err = journal_end(&th, sb, 2);
2042 if (!ret && err)
2043 ret = err;
2044 out:
2045 reiserfs_write_unlock(sb);
2046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049/*
Christoph Hellwig84de8562005-06-23 00:09:16 -07002050 * Turn on quotas during mount time - we need to find the quota file and such...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
2052static int reiserfs_quota_on_mount(struct super_block *sb, int type)
2053{
Christoph Hellwig84de8562005-06-23 00:09:16 -07002054 return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002055 REISERFS_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
2058/*
2059 * Standard function to be called on quota_on
2060 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002061static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
Jan Kara1b445a92008-04-28 02:14:35 -07002062 char *path, int remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002064 int err;
2065 struct nameidata nd;
Jan Karad5dee5c2008-04-28 02:16:23 -07002066 struct inode *inode;
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002067 struct reiserfs_transaction_handle th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002069 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
2070 return -EINVAL;
Jan Kara1b445a92008-04-28 02:14:35 -07002071 /* No more checks needed? Path and format_id are bogus anyway... */
2072 if (remount)
2073 return vfs_quota_on(sb, type, format_id, path, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002074 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2075 if (err)
2076 return err;
2077 /* Quotafile not on the same filesystem? */
Jan Blunck4ac91372008-02-14 19:34:32 -08002078 if (nd.path.mnt->mnt_sb != sb) {
Jan Blunck1d957f92008-02-14 19:34:35 -08002079 path_put(&nd.path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002080 return -EXDEV;
2081 }
Jan Karad5dee5c2008-04-28 02:16:23 -07002082 inode = nd.path.dentry->d_inode;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002083 /* We must not pack tails for quota files on reiserfs for quota IO to work */
Jan Karad5dee5c2008-04-28 02:16:23 -07002084 if (!(REISERFS_I(inode)->i_flags & i_nopack_mask)) {
2085 err = reiserfs_unpack(inode, NULL);
2086 if (err) {
2087 reiserfs_warning(sb,
2088 "reiserfs: Unpacking tail of quota file failed"
2089 " (%d). Cannot turn on quotas.", err);
2090 path_put(&nd.path);
2091 return -EINVAL;
2092 }
2093 mark_inode_dirty(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002094 }
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002095 /* Journaling quota? */
2096 if (REISERFS_SB(sb)->s_qf_names[type]) {
2097 /* Quotafile not of fs root? */
2098 if (nd.path.dentry->d_parent->d_inode != sb->s_root->d_inode)
2099 reiserfs_warning(sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002100 "reiserfs: Quota file not on filesystem root. "
2101 "Journalled quota will not work.");
Jan Kara5d4f7fd2008-07-25 01:46:36 -07002102 }
2103
2104 /*
2105 * When we journal data on quota file, we have to flush journal to see
2106 * all updates to the file when we bypass pagecache...
2107 */
2108 if (reiserfs_file_data_log(inode)) {
2109 /* Just start temporary transaction and finish it */
2110 err = journal_begin(&th, sb, 1);
2111 if (err)
2112 return err;
2113 err = journal_end_sync(&th, sb, 1);
2114 if (err)
2115 return err;
2116 }
Jan Blunck1d957f92008-02-14 19:34:35 -08002117 path_put(&nd.path);
Jan Kara1b445a92008-04-28 02:14:35 -07002118 return vfs_quota_on(sb, type, format_id, path, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121/* Read data from quotafile - avoid pagecache and such because we cannot afford
2122 * acquiring the locks... As quota files are never truncated and quota code
2123 * itself serializes the operations (and noone else should touch the files)
2124 * we don't have to be afraid of races */
2125static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2126 size_t len, loff_t off)
2127{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002128 struct inode *inode = sb_dqopt(sb)->files[type];
2129 unsigned long blk = off >> sb->s_blocksize_bits;
2130 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2131 size_t toread;
2132 struct buffer_head tmp_bh, *bh;
2133 loff_t i_size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002135 if (off > i_size)
2136 return 0;
2137 if (off + len > i_size)
2138 len = i_size - off;
2139 toread = len;
2140 while (toread > 0) {
2141 tocopy =
2142 sb->s_blocksize - offset <
2143 toread ? sb->s_blocksize - offset : toread;
2144 tmp_bh.b_state = 0;
2145 /* Quota files are without tails so we can safely use this function */
2146 reiserfs_write_lock(sb);
2147 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2148 reiserfs_write_unlock(sb);
2149 if (err)
2150 return err;
2151 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2152 memset(data, 0, tocopy);
2153 else {
2154 bh = sb_bread(sb, tmp_bh.b_blocknr);
2155 if (!bh)
2156 return -EIO;
2157 memcpy(data, bh->b_data + offset, tocopy);
2158 brelse(bh);
2159 }
2160 offset = 0;
2161 toread -= tocopy;
2162 data += tocopy;
2163 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002165 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
2168/* Write to quotafile (we know the transaction is already started and has
2169 * enough credits) */
2170static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2171 const char *data, size_t len, loff_t off)
2172{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002173 struct inode *inode = sb_dqopt(sb)->files[type];
2174 unsigned long blk = off >> sb->s_blocksize_bits;
2175 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2176 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2177 size_t towrite = len;
2178 struct buffer_head tmp_bh, *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Jan Kara9c3013e2007-09-11 15:23:29 -07002180 if (!current->journal_info) {
2181 printk(KERN_WARNING "reiserfs: Quota write (off=%Lu, len=%Lu)"
2182 " cancelled because transaction is not started.\n",
2183 (unsigned long long)off, (unsigned long long)len);
2184 return -EIO;
2185 }
Arjan van de Ven5c81a412006-07-03 00:25:20 -07002186 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002187 while (towrite > 0) {
2188 tocopy = sb->s_blocksize - offset < towrite ?
2189 sb->s_blocksize - offset : towrite;
2190 tmp_bh.b_state = 0;
2191 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2192 if (err)
2193 goto out;
2194 if (offset || tocopy != sb->s_blocksize)
2195 bh = sb_bread(sb, tmp_bh.b_blocknr);
2196 else
2197 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2198 if (!bh) {
2199 err = -EIO;
2200 goto out;
2201 }
2202 lock_buffer(bh);
2203 memcpy(bh->b_data + offset, data, tocopy);
2204 flush_dcache_page(bh->b_page);
2205 set_buffer_uptodate(bh);
2206 unlock_buffer(bh);
2207 reiserfs_prepare_for_journal(sb, bh, 1);
2208 journal_mark_dirty(current->journal_info, sb, bh);
2209 if (!journal_quota)
2210 reiserfs_add_ordered_list(inode, bh);
2211 brelse(bh);
2212 offset = 0;
2213 towrite -= tocopy;
2214 data += tocopy;
2215 blk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
Jan Kara9c3013e2007-09-11 15:23:29 -07002217out:
Jan Kara10dd08d2008-07-04 09:59:34 -07002218 if (len == towrite) {
2219 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002220 return err;
Jan Kara10dd08d2008-07-04 09:59:34 -07002221 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002222 if (inode->i_size < off + len - towrite)
2223 i_size_write(inode, off + len - towrite);
2224 inode->i_version++;
2225 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2226 mark_inode_dirty(inode);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002227 mutex_unlock(&inode->i_mutex);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002228 return len - towrite;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231#endif
2232
David Howells454e2392006-06-23 02:02:57 -07002233static int get_super_block(struct file_system_type *fs_type,
2234 int flags, const char *dev_name,
2235 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
David Howells454e2392006-06-23 02:02:57 -07002237 return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
2238 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002241static int __init init_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 int ret;
2244
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002245 if ((ret = init_inodecache())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return ret;
2247 }
2248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002249 if ((ret = reiserfs_xattr_register_handlers()))
2250 goto failed_reiserfs_xattr_register_handlers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002252 reiserfs_proc_info_global_init();
2253 reiserfs_proc_register_global("version",
2254 reiserfs_global_version_in_proc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002256 ret = register_filesystem(&reiserfs_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 if (ret == 0) {
2259 return 0;
2260 }
2261
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002262 reiserfs_xattr_unregister_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002264 failed_reiserfs_xattr_register_handlers:
2265 reiserfs_proc_unregister_global("version");
2266 reiserfs_proc_info_global_done();
2267 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 return ret;
2270}
2271
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002272static void __exit exit_reiserfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002274 reiserfs_xattr_unregister_handlers();
2275 reiserfs_proc_unregister_global("version");
2276 reiserfs_proc_info_global_done();
2277 unregister_filesystem(&reiserfs_fs_type);
2278 destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
2281struct file_system_type reiserfs_fs_type = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002282 .owner = THIS_MODULE,
2283 .name = "reiserfs",
2284 .get_sb = get_super_block,
David Howellsedc666e2006-10-11 01:22:14 -07002285 .kill_sb = reiserfs_kill_sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002286 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287};
2288
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002289MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2290MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2291MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002293module_init(init_reiserfs_fs);
2294module_exit(exit_reiserfs_fs);