1 From: Jeff Mahoney <jeffm@suse.com>
2 Subject: reiserfs: journaled xattrs
4 Deadlocks are possible in the xattr code between the journal lock and the
7 This patch implements journalling for xattr operations. The benefit is
9 * It gets rid of the deadlock possibility by always ensuring that xattr
10 write operations are initiated inside a transaction.
11 * It corrects the problem where xattr backing files aren't considered any
12 differently than normal files, despite the fact they are metadata.
14 I discussed the added journal load with Chris Mason, and we decided that
15 since xattrs (versus other journal activity) is fairly rare, the introduction
16 of larger transactions to support journaled xattrs wouldn't be too big a deal.
18 Signed-off-by: Jeff Mahoney <jeffm@suse.com>
21 fs/reiserfs/inode.c | 3 -
22 fs/reiserfs/namei.c | 14 +----
23 fs/reiserfs/xattr.c | 39 +++++++++++----
24 fs/reiserfs/xattr_acl.c | 105 +++++++++++++++++++++++++++++++----------
25 include/linux/reiserfs_acl.h | 3 -
26 include/linux/reiserfs_fs.h | 4 +
27 include/linux/reiserfs_xattr.h | 40 ++++++++++++++-
28 7 files changed, 159 insertions(+), 49 deletions(-)
30 --- a/fs/reiserfs/inode.c
31 +++ b/fs/reiserfs/inode.c
32 @@ -1919,9 +1919,8 @@ int reiserfs_new_inode(struct reiserfs_t
36 - /* XXX CHECK THIS */
37 if (reiserfs_posixacl(inode->i_sb)) {
38 - retval = reiserfs_inherit_default_acl(dir, dentry, inode);
39 + retval = reiserfs_inherit_default_acl(th, dir, dentry, inode);
42 reiserfs_check_path(&path_to_key);
43 --- a/fs/reiserfs/namei.c
44 +++ b/fs/reiserfs/namei.c
45 @@ -607,15 +607,13 @@ static int reiserfs_create(struct inode
46 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
47 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
48 struct reiserfs_transaction_handle th;
51 if (!(inode = new_inode(dir->i_sb))) {
54 new_inode_init(inode, dir, mode);
56 - locked = reiserfs_cache_default_acl(dir);
58 + jbegin_count += reiserfs_cache_default_acl(dir);
59 reiserfs_write_lock(dir->i_sb);
61 retval = journal_begin(&th, dir->i_sb, jbegin_count);
62 @@ -669,7 +667,6 @@ static int reiserfs_mknod(struct inode *
63 JOURNAL_PER_BALANCE_CNT * 3 +
64 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
65 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
68 if (!new_valid_dev(rdev))
70 @@ -679,8 +676,7 @@ static int reiserfs_mknod(struct inode *
72 new_inode_init(inode, dir, mode);
74 - locked = reiserfs_cache_default_acl(dir);
76 + jbegin_count += reiserfs_cache_default_acl(dir);
77 reiserfs_write_lock(dir->i_sb);
79 retval = journal_begin(&th, dir->i_sb, jbegin_count);
80 @@ -737,7 +733,6 @@ static int reiserfs_mkdir(struct inode *
81 JOURNAL_PER_BALANCE_CNT * 3 +
82 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
83 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
86 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
87 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
88 @@ -749,8 +744,7 @@ static int reiserfs_mkdir(struct inode *
90 new_inode_init(inode, dir, mode);
92 - locked = reiserfs_cache_default_acl(dir);
94 + jbegin_count += reiserfs_cache_default_acl(dir);
95 reiserfs_write_lock(dir->i_sb);
97 retval = journal_begin(&th, dir->i_sb, jbegin_count);
98 @@ -1037,8 +1031,6 @@ static int reiserfs_symlink(struct inode
99 memcpy(name, symname, strlen(symname));
100 padd_item(name, item_len, strlen(symname));
102 - /* We would inherit the default ACL here, but symlinks don't get ACLs */
104 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
106 drop_new_inode(inode);
107 --- a/fs/reiserfs/xattr_acl.c
108 +++ b/fs/reiserfs/xattr_acl.c
110 #include <linux/reiserfs_acl.h>
111 #include <asm/uaccess.h>
113 -static int reiserfs_set_acl(struct inode *inode, int type,
114 +static int reiserfs_set_acl(struct reiserfs_transaction_handle *th,
115 + struct inode *inode, int type,
116 struct posix_acl *acl);
119 xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
121 struct posix_acl *acl;
125 + struct reiserfs_transaction_handle th;
126 + size_t jcreate_blocks;
127 if (!reiserfs_posixacl(inode->i_sb))
129 if (!is_owner_or_cap(inode))
130 @@ -36,7 +38,21 @@ xattr_set_acl(struct inode *inode, int t
134 - error = reiserfs_set_acl(inode, type, acl);
135 + /* Pessimism: We can't assume that anything from the xattr root up
136 + * has been created. */
138 + jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
139 + reiserfs_xattr_nblocks(inode, size) * 2;
141 + reiserfs_write_lock(inode->i_sb);
142 + error = journal_begin(&th, inode->i_sb, jcreate_blocks);
144 + error = reiserfs_set_acl(&th, inode, type, acl);
145 + error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
149 + reiserfs_write_unlock(inode->i_sb);
152 posix_acl_release(acl);
153 @@ -266,7 +282,8 @@ struct posix_acl *reiserfs_get_acl(struc
154 * BKL held [before 2.5.x]
157 -reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
158 +reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
159 + int type, struct posix_acl *acl)
163 @@ -310,7 +327,7 @@ reiserfs_set_acl(struct inode *inode, in
164 return (int)PTR_ERR(value);
167 - error = __reiserfs_xattr_set(inode, name, value, size, 0);
168 + error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
171 * Ensure that the inode gets dirtied if we're only using
172 @@ -337,7 +354,8 @@ reiserfs_set_acl(struct inode *inode, in
173 /* dir->i_mutex: locked,
174 * inode is new and not released into the wild yet */
176 -reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
177 +reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
178 + struct inode *dir, struct dentry *dentry,
181 struct posix_acl *acl;
182 @@ -374,7 +392,8 @@ reiserfs_inherit_default_acl(struct inod
184 /* Copy the default ACL to the default ACL of a new directory */
185 if (S_ISDIR(inode->i_mode)) {
186 - err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
187 + err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
192 @@ -395,9 +414,9 @@ reiserfs_inherit_default_acl(struct inod
194 /* If we need an ACL.. */
197 - reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
199 + err = reiserfs_set_acl(th, inode,
205 @@ -415,21 +434,45 @@ reiserfs_inherit_default_acl(struct inod
209 -/* Looks up and caches the result of the default ACL.
210 - * We do this so that we don't need to carry the xattr_sem into
211 - * reiserfs_new_inode if we don't need to */
212 +/* This is used to cache the default acl before a new object is created.
213 + * The biggest reason for this is to get an idea of how many blocks will
214 + * actually be required for the create operation if we must inherit an ACL.
215 + * An ACL write can add up to 3 object creations and an additional file write
216 + * so we'd prefer not to reserve that many blocks in the journal if we can.
217 + * It also has the advantage of not loading the ACL with a transaction open,
218 + * this may seem silly, but if the owner of the directory is doing the
219 + * creation, the ACL may not be loaded since the permissions wouldn't require
221 + * We return the number of blocks required for the transaction.
223 int reiserfs_cache_default_acl(struct inode *inode)
226 - if (reiserfs_posixacl(inode->i_sb) && !IS_PRIVATE(inode)) {
227 - struct posix_acl *acl;
228 - acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
229 - ret = (acl && !IS_ERR(acl));
231 - posix_acl_release(acl);
232 + struct posix_acl *acl;
235 + if (IS_PRIVATE(inode))
238 + acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
240 + if (acl && !IS_ERR(acl)) {
241 + int size = reiserfs_acl_size(acl->a_count);
243 + /* Other xattrs can be created during inode creation. We don't
244 + * want to claim too many blocks, so we check to see if we
245 + * we need to create the tree to the xattrs, and then we
246 + * just want two files. */
247 + nblocks = reiserfs_xattr_jcreate_nblocks(inode);
248 + nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
250 + REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
252 + /* We need to account for writes + bitmaps for two files */
253 + nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
254 + posix_acl_release(acl);
261 int reiserfs_acl_chmod(struct inode *inode)
262 @@ -455,8 +498,22 @@ int reiserfs_acl_chmod(struct inode *ino
265 error = posix_acl_chmod_masq(clone, inode->i_mode);
267 - error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
269 + struct reiserfs_transaction_handle th;
270 + size_t size = reiserfs_xattr_nblocks(inode,
271 + reiserfs_acl_size(clone->a_count));
272 + reiserfs_write_lock(inode->i_sb);
273 + error = journal_begin(&th, inode->i_sb, size * 2);
276 + error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS,
278 + error2 = journal_end(&th, inode->i_sb, size * 2);
282 + reiserfs_write_unlock(inode->i_sb);
284 posix_acl_release(clone);
287 --- a/fs/reiserfs/xattr.c
288 +++ b/fs/reiserfs/xattr.c
289 @@ -632,8 +632,9 @@ out_dput:
290 * inode->i_mutex: down
293 -__reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
294 - size_t buffer_size, int flags)
295 +reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
296 + struct inode *inode, const char *name,
297 + const void *buffer, size_t buffer_size, int flags)
300 struct dentry *dentry;
301 @@ -723,14 +724,34 @@ out_unlock:
306 -reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
307 - size_t buffer_size, int flags)
308 +/* We need to start a transaction to maintain lock ordering */
309 +int reiserfs_xattr_set(struct inode *inode, const char *name,
310 + const void *buffer, size_t buffer_size, int flags)
312 - int err = __reiserfs_xattr_set(inode, name, buffer, buffer_size, flags);
313 - if (err == -ENODATA)
317 + struct reiserfs_transaction_handle th;
319 + size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
321 + if (!(flags & XATTR_REPLACE))
322 + jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
324 + reiserfs_write_lock(inode->i_sb);
325 + error = journal_begin(&th, inode->i_sb, jbegin_count);
327 + reiserfs_write_unlock(inode->i_sb);
331 + error = reiserfs_xattr_set_handle(&th, inode, name,
332 + buffer, buffer_size, flags);
334 + error2 = journal_end(&th, inode->i_sb, jbegin_count);
337 + reiserfs_write_unlock(inode->i_sb);
343 --- a/include/linux/reiserfs_acl.h
344 +++ b/include/linux/reiserfs_acl.h
345 @@ -49,7 +49,8 @@ static inline int reiserfs_acl_count(siz
346 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
347 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type);
348 int reiserfs_acl_chmod(struct inode *inode);
349 -int reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
350 +int reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
351 + struct inode *dir, struct dentry *dentry,
352 struct inode *inode);
353 int reiserfs_cache_default_acl(struct inode *dir);
354 extern struct xattr_handler reiserfs_posix_acl_default_handler;
355 --- a/include/linux/reiserfs_fs.h
356 +++ b/include/linux/reiserfs_fs.h
357 @@ -1615,6 +1615,10 @@ struct reiserfs_journal_header {
358 #define JOURNAL_MAX_COMMIT_AGE 30
359 #define JOURNAL_MAX_TRANS_AGE 30
360 #define JOURNAL_PER_BALANCE_CNT (3 * (MAX_HEIGHT-2) + 9)
361 +#define JOURNAL_BLOCKS_PER_OBJECT(sb) (JOURNAL_PER_BALANCE_CNT * 3 + \
362 + 2 * (REISERFS_QUOTA_INIT_BLOCKS(sb) + \
363 + REISERFS_QUOTA_TRANS_BLOCKS(sb)))
366 /* We need to update data and inode (atime) */
367 #define REISERFS_QUOTA_TRANS_BLOCKS(s) (REISERFS_SB(s)->s_mount_opt & (1<<REISERFS_QUOTA) ? 2 : 0)
368 --- a/include/linux/reiserfs_xattr.h
369 +++ b/include/linux/reiserfs_xattr.h
370 @@ -46,14 +46,50 @@ int reiserfs_removexattr(struct dentry *
371 int reiserfs_permission(struct inode *inode, int mask);
373 int reiserfs_xattr_get(struct inode *, const char *, void *, size_t);
374 -int __reiserfs_xattr_set(struct inode *, const char *, const void *,
376 int reiserfs_xattr_set(struct inode *, const char *, const void *, size_t, int);
377 +int reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *,
378 + struct inode *, const char *, const void *,
381 extern struct xattr_handler reiserfs_xattr_user_handler;
382 extern struct xattr_handler reiserfs_xattr_trusted_handler;
383 extern struct xattr_handler reiserfs_xattr_security_handler;
385 +#define xattr_size(size) ((size) + sizeof(struct reiserfs_xattr_header))
386 +static inline loff_t reiserfs_xattr_nblocks(struct inode *inode, loff_t size)
389 + if (reiserfs_file_data_log(inode)) {
390 + ret = _ROUND_UP(xattr_size(size), inode->i_sb->s_blocksize);
391 + ret >>= inode->i_sb->s_blocksize_bits;
396 +/* We may have to create up to 3 objects: xattr root, xattr dir, xattr file.
397 + * Let's try to be smart about it.
398 + * xattr root: We cache it. If it's not cached, we may need to create it.
399 + * xattr dir: If anything has been loaded for this inode, we can set a flag
401 + * xattr file: Since we don't cache xattrs, we can't tell. We always include
404 + * However, since root and dir can be created between calls - YOU MUST SAVE
407 +static inline size_t reiserfs_xattr_jcreate_nblocks(struct inode *inode)
409 + size_t nblocks = JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
411 + if ((REISERFS_I(inode)->i_flags & i_has_xattr_dir) == 0) {
412 + nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
413 + if (REISERFS_SB(inode->i_sb)->xattr_root == NULL)
414 + nblocks += JOURNAL_BLOCKS_PER_OBJECT(inode->i_sb);
420 static inline void reiserfs_init_xattr_rwsem(struct inode *inode)
422 init_rwsem(&REISERFS_I(inode)->i_xattr_sem);