Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/reiserfs/xattr.c |
| 3 | * |
| 4 | * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * In order to implement EA/ACLs in a clean, backwards compatible manner, |
| 10 | * they are implemented as files in a "private" directory. |
| 11 | * Each EA is in it's own file, with the directory layout like so (/ is assumed |
| 12 | * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory, |
| 13 | * directories named using the capital-hex form of the objectid and |
| 14 | * generation number are used. Inside each directory are individual files |
| 15 | * named with the name of the extended attribute. |
| 16 | * |
| 17 | * So, for objectid 12648430, we could have: |
| 18 | * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access |
| 19 | * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default |
| 20 | * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type |
| 21 | * .. or similar. |
| 22 | * |
| 23 | * The file contents are the text of the EA. The size is known based on the |
| 24 | * stat data describing the file. |
| 25 | * |
| 26 | * In the case of system.posix_acl_access and system.posix_acl_default, since |
| 27 | * these are special cases for filesystem ACLs, they are interpreted by the |
| 28 | * kernel, in addition, they are negatively and positively cached and attached |
| 29 | * to the inode so that unnecessary lookups are avoided. |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame^] | 30 | * |
| 31 | * Locking works like so: |
| 32 | * The xattr root (/.reiserfs_priv/xattrs) is protected by its i_mutex. |
| 33 | * The xattr dir (/.reiserfs_priv/xattrs/<oid>.<gen>) is protected by |
| 34 | * inode->xattr_sem. |
| 35 | * The xattrs themselves are likewise protected by the xattr_sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #include <linux/reiserfs_fs.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 39 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/dcache.h> |
| 41 | #include <linux/namei.h> |
| 42 | #include <linux/errno.h> |
| 43 | #include <linux/fs.h> |
| 44 | #include <linux/file.h> |
| 45 | #include <linux/pagemap.h> |
| 46 | #include <linux/xattr.h> |
| 47 | #include <linux/reiserfs_xattr.h> |
| 48 | #include <linux/reiserfs_acl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <asm/uaccess.h> |
Al Viro | 3277c39 | 2006-11-14 21:13:53 -0800 | [diff] [blame] | 50 | #include <net/checksum.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/smp_lock.h> |
| 52 | #include <linux/stat.h> |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 53 | #include <linux/quotaops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #define PRIVROOT_NAME ".reiserfs_priv" |
| 56 | #define XAROOT_NAME "xattrs" |
| 57 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 58 | /* Helpers for inode ops. We do this so that we don't have all the VFS |
| 59 | * overhead and also for proper i_mutex annotation. |
| 60 | * dir->i_mutex must be held for all of them. */ |
| 61 | static int xattr_create(struct inode *dir, struct dentry *dentry, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 63 | BUG_ON(!mutex_is_locked(&dir->i_mutex)); |
| 64 | DQUOT_INIT(dir); |
| 65 | return dir->i_op->create(dir, dentry, mode, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 68 | static int xattr_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 69 | { |
| 70 | BUG_ON(!mutex_is_locked(&dir->i_mutex)); |
| 71 | DQUOT_INIT(dir); |
| 72 | return dir->i_op->mkdir(dir, dentry, mode); |
| 73 | } |
| 74 | |
| 75 | /* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr |
| 76 | * mutation ops aren't called during rename or splace, which are the |
| 77 | * only other users of I_MUTEX_CHILD. It violates the ordering, but that's |
| 78 | * better than allocating another subclass just for this code. */ |
| 79 | static int xattr_unlink(struct inode *dir, struct dentry *dentry) |
| 80 | { |
| 81 | int error; |
| 82 | BUG_ON(!mutex_is_locked(&dir->i_mutex)); |
| 83 | DQUOT_INIT(dir); |
| 84 | |
| 85 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); |
| 86 | error = dir->i_op->unlink(dir, dentry); |
| 87 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 88 | |
| 89 | if (!error) |
| 90 | d_delete(dentry); |
| 91 | return error; |
| 92 | } |
| 93 | |
| 94 | static int xattr_rmdir(struct inode *dir, struct dentry *dentry) |
| 95 | { |
| 96 | int error; |
| 97 | BUG_ON(!mutex_is_locked(&dir->i_mutex)); |
| 98 | DQUOT_INIT(dir); |
| 99 | |
| 100 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD); |
| 101 | dentry_unhash(dentry); |
| 102 | error = dir->i_op->rmdir(dir, dentry); |
| 103 | if (!error) |
| 104 | dentry->d_inode->i_flags |= S_DEAD; |
| 105 | mutex_unlock(&dentry->d_inode->i_mutex); |
| 106 | if (!error) |
| 107 | d_delete(dentry); |
| 108 | dput(dentry); |
| 109 | |
| 110 | return error; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE) |
| 115 | |
| 116 | /* Returns and possibly creates the xattr dir. */ |
| 117 | static struct dentry *lookup_or_create_dir(struct dentry *parent, |
| 118 | const char *name, int flags) |
| 119 | { |
| 120 | struct dentry *dentry; |
| 121 | BUG_ON(!parent); |
| 122 | |
| 123 | dentry = lookup_one_len(name, parent, strlen(name)); |
| 124 | if (IS_ERR(dentry)) |
| 125 | return dentry; |
| 126 | else if (!dentry->d_inode) { |
| 127 | int err = -ENODATA; |
| 128 | |
| 129 | if (xattr_may_create(flags)) { |
| 130 | mutex_lock_nested(&parent->d_inode->i_mutex, |
| 131 | I_MUTEX_XATTR); |
| 132 | err = xattr_mkdir(parent->d_inode, dentry, 0700); |
| 133 | mutex_unlock(&parent->d_inode->i_mutex); |
| 134 | } |
| 135 | |
| 136 | if (err) { |
| 137 | dput(dentry); |
| 138 | dentry = ERR_PTR(err); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return dentry; |
| 143 | } |
| 144 | |
| 145 | static struct dentry *open_xa_root(struct super_block *sb, int flags) |
| 146 | { |
| 147 | struct dentry *privroot = REISERFS_SB(sb)->priv_root; |
| 148 | if (!privroot) |
| 149 | return ERR_PTR(-ENODATA); |
| 150 | return lookup_or_create_dir(privroot, XAROOT_NAME, flags); |
| 151 | } |
| 152 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 153 | static struct dentry *open_xa_dir(const struct inode *inode, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 155 | struct dentry *xaroot, *xadir; |
| 156 | char namebuf[17]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 158 | xaroot = open_xa_root(inode->i_sb, flags); |
Jeff Mahoney | 9b7f375 | 2007-04-23 14:41:17 -0700 | [diff] [blame] | 159 | if (IS_ERR(xaroot)) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 160 | return xaroot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 162 | snprintf(namebuf, sizeof(namebuf), "%X.%X", |
| 163 | le32_to_cpu(INODE_PKEY(inode)->k_objectid), |
| 164 | inode->i_generation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 166 | xadir = lookup_or_create_dir(xaroot, namebuf, flags); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 167 | dput(xaroot); |
| 168 | return xadir; |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | /* |
| 173 | * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but |
| 174 | * we need to drop the path before calling the filldir struct. That |
| 175 | * would be a big performance hit to the non-xattr case, so I've copied |
| 176 | * the whole thing for now. --clm |
| 177 | * |
| 178 | * the big difference is that I go backwards through the directory, |
| 179 | * and don't mess with f->f_pos, but the idea is the same. Do some |
| 180 | * action on each and every entry in the directory. |
| 181 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 182 | * we're called with i_mutex held, so there are no worries about the directory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * changing underneath us. |
| 184 | */ |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 185 | static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 187 | struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */ |
| 188 | INITIALIZE_PATH(path_to_entry); |
| 189 | struct buffer_head *bh; |
| 190 | int entry_num; |
| 191 | struct item_head *ih, tmp_ih; |
| 192 | int search_res; |
| 193 | char *local_buf; |
| 194 | loff_t next_pos; |
| 195 | char small_buf[32]; /* avoid kmalloc if we can */ |
| 196 | struct reiserfs_de_head *deh; |
| 197 | int d_reclen; |
| 198 | char *d_name; |
| 199 | off_t d_off; |
| 200 | ino_t d_ino; |
| 201 | struct reiserfs_dir_entry de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 203 | /* form key for search the next directory entry using f_pos field of |
| 204 | file structure */ |
| 205 | next_pos = max_reiserfs_offset(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 207 | while (1) { |
| 208 | research: |
| 209 | if (next_pos <= DOT_DOT_OFFSET) |
| 210 | break; |
| 211 | make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 213 | search_res = |
| 214 | search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry, |
| 215 | &de); |
| 216 | if (search_res == IO_ERROR) { |
| 217 | // FIXME: we could just skip part of directory which could |
| 218 | // not be read |
| 219 | pathrelse(&path_to_entry); |
| 220 | return -EIO; |
| 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 223 | if (search_res == NAME_NOT_FOUND) |
| 224 | de.de_entry_num--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 226 | set_de_name_and_namelen(&de); |
| 227 | entry_num = de.de_entry_num; |
| 228 | deh = &(de.de_deh[entry_num]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 230 | bh = de.de_bh; |
| 231 | ih = de.de_ih; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 233 | if (!is_direntry_le_ih(ih)) { |
Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 234 | reiserfs_error(inode->i_sb, "jdm-20000", |
| 235 | "not direntry %h", ih); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 236 | break; |
| 237 | } |
| 238 | copy_item_head(&tmp_ih, ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 240 | /* we must have found item, that is item of this directory, */ |
| 241 | RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key), |
| 242 | "vs-9000: found item %h does not match to dir we readdir %K", |
| 243 | ih, &pos_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 245 | if (deh_offset(deh) <= DOT_DOT_OFFSET) { |
| 246 | break; |
| 247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 249 | /* look for the previous entry in the directory */ |
| 250 | next_pos = deh_offset(deh) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 252 | if (!de_visible(deh)) |
| 253 | /* it is hidden entry */ |
| 254 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 256 | d_reclen = entry_length(bh, ih, entry_num); |
| 257 | d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh); |
| 258 | d_off = deh_offset(deh); |
| 259 | d_ino = deh_objectid(deh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 261 | if (!d_name[d_reclen - 1]) |
| 262 | d_reclen = strlen(d_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 264 | if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) { |
| 265 | /* too big to send back to VFS */ |
| 266 | continue; |
| 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 269 | /* Ignore the .reiserfs_priv entry */ |
| 270 | if (reiserfs_xattrs(inode->i_sb) && |
| 271 | !old_format_only(inode->i_sb) && |
| 272 | deh_objectid(deh) == |
| 273 | le32_to_cpu(INODE_PKEY |
| 274 | (REISERFS_SB(inode->i_sb)->priv_root->d_inode)-> |
| 275 | k_objectid)) |
| 276 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 278 | if (d_reclen <= 32) { |
| 279 | local_buf = small_buf; |
| 280 | } else { |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 281 | local_buf = kmalloc(d_reclen, GFP_NOFS); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 282 | if (!local_buf) { |
| 283 | pathrelse(&path_to_entry); |
| 284 | return -ENOMEM; |
| 285 | } |
| 286 | if (item_moved(&tmp_ih, &path_to_entry)) { |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 287 | kfree(local_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 289 | /* sigh, must retry. Do this same offset again */ |
| 290 | next_pos = d_off; |
| 291 | goto research; |
| 292 | } |
| 293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 295 | // Note, that we copy name to user space via temporary |
| 296 | // buffer (local_buf) because filldir will block if |
| 297 | // user space buffer is swapped out. At that time |
| 298 | // entry can move to somewhere else |
| 299 | memcpy(local_buf, d_name, d_reclen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 301 | /* the filldir function might need to start transactions, |
| 302 | * or do who knows what. Release the path now that we've |
| 303 | * copied all the important stuff out of the deh |
| 304 | */ |
| 305 | pathrelse(&path_to_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 307 | if (filldir(dirent, local_buf, d_reclen, d_off, d_ino, |
| 308 | DT_UNKNOWN) < 0) { |
| 309 | if (local_buf != small_buf) { |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 310 | kfree(local_buf); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 311 | } |
| 312 | goto end; |
| 313 | } |
| 314 | if (local_buf != small_buf) { |
Pekka Enberg | d739b42 | 2006-02-01 03:06:43 -0800 | [diff] [blame] | 315 | kfree(local_buf); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 316 | } |
| 317 | } /* while */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 319 | end: |
| 320 | pathrelse(&path_to_entry); |
| 321 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * this could be done with dedicated readdir ops for the xattr files, |
| 326 | * but I want to get something working asap |
| 327 | * this is stolen from vfs_readdir |
| 328 | * |
| 329 | */ |
| 330 | static |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 331 | int xattr_readdir(struct inode *inode, filldir_t filler, void *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 333 | int res = -ENOENT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 334 | if (!IS_DEADDIR(inode)) { |
| 335 | lock_kernel(); |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 336 | res = __xattr_readdir(inode, buf, filler); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 337 | unlock_kernel(); |
| 338 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 339 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 342 | static int |
| 343 | __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen) |
| 344 | { |
| 345 | struct dentry *dentry; |
| 346 | struct inode *dir = xadir->d_inode; |
| 347 | int err = 0; |
| 348 | |
| 349 | dentry = lookup_one_len(name, xadir, namelen); |
| 350 | if (IS_ERR(dentry)) { |
| 351 | err = PTR_ERR(dentry); |
| 352 | goto out; |
| 353 | } else if (!dentry->d_inode) { |
| 354 | err = -ENODATA; |
| 355 | goto out_file; |
| 356 | } |
| 357 | |
| 358 | /* Skip directories.. */ |
| 359 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 360 | goto out_file; |
| 361 | |
| 362 | if (!IS_PRIVATE(dentry->d_inode)) { |
| 363 | reiserfs_error(dir->i_sb, "jdm-20003", |
| 364 | "OID %08x [%.*s/%.*s] doesn't have " |
| 365 | "priv flag set [parent is %sset].", |
| 366 | le32_to_cpu(INODE_PKEY(dentry->d_inode)-> |
| 367 | k_objectid), xadir->d_name.len, |
| 368 | xadir->d_name.name, namelen, name, |
| 369 | IS_PRIVATE(xadir->d_inode) ? "" : |
| 370 | "not "); |
| 371 | dput(dentry); |
| 372 | return -EIO; |
| 373 | } |
| 374 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 375 | err = xattr_unlink(dir, dentry); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 376 | |
| 377 | out_file: |
| 378 | dput(dentry); |
| 379 | |
| 380 | out: |
| 381 | return err; |
| 382 | } |
| 383 | |
| 384 | /* The following are side effects of other operations that aren't explicitly |
| 385 | * modifying extended attributes. This includes operations such as permissions |
| 386 | * or ownership changes, object deletions, etc. */ |
| 387 | |
| 388 | static int |
| 389 | reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen, |
| 390 | loff_t offset, u64 ino, unsigned int d_type) |
| 391 | { |
| 392 | struct dentry *xadir = (struct dentry *)buf; |
| 393 | |
| 394 | return __reiserfs_xattr_del(xadir, name, namelen); |
| 395 | |
| 396 | } |
| 397 | |
| 398 | /* This is called w/ inode->i_mutex downed */ |
| 399 | int reiserfs_delete_xattrs(struct inode *inode) |
| 400 | { |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 401 | int err = 0; |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame^] | 402 | struct dentry *dir, *root; |
| 403 | struct reiserfs_transaction_handle th; |
| 404 | int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 + |
| 405 | 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 406 | |
| 407 | /* Skip out, an xattr has no xattrs associated with it */ |
| 408 | if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1) |
| 409 | return 0; |
| 410 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 411 | dir = open_xa_dir(inode, XATTR_REPLACE); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 412 | if (IS_ERR(dir)) { |
| 413 | err = PTR_ERR(dir); |
| 414 | goto out; |
| 415 | } else if (!dir->d_inode) { |
| 416 | dput(dir); |
| 417 | return 0; |
| 418 | } |
| 419 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 420 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 421 | err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 422 | mutex_unlock(&dir->d_inode->i_mutex); |
| 423 | if (err) |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 424 | goto out_dir; |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 425 | |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame^] | 426 | /* We start a transaction here to avoid a ABBA situation |
| 427 | * between the xattr root's i_mutex and the journal lock. |
| 428 | * Inode creation will inherit an ACL, which requires a |
| 429 | * lookup. The lookup locks the xattr root i_mutex with a |
| 430 | * transaction open. Inode deletion takes teh xattr root |
| 431 | * i_mutex to delete the directory and then starts a |
| 432 | * transaction inside it. Boom. This doesn't incur much |
| 433 | * additional overhead since the reiserfs_rmdir transaction |
| 434 | * will just nest inside the outer transaction. */ |
| 435 | err = journal_begin(&th, inode->i_sb, blocks); |
| 436 | if (!err) { |
| 437 | int jerror; |
| 438 | root = dget(dir->d_parent); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 439 | mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_XATTR); |
| 440 | err = xattr_rmdir(root->d_inode, dir); |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame^] | 441 | jerror = journal_end(&th, inode->i_sb, blocks); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 442 | mutex_unlock(&root->d_inode->i_mutex); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 443 | dput(root); |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame^] | 444 | |
| 445 | err = jerror ?: err; |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 446 | } |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 447 | |
| 448 | out_dir: |
| 449 | dput(dir); |
| 450 | |
| 451 | out: |
| 452 | if (!err) |
| 453 | REISERFS_I(inode)->i_flags = |
| 454 | REISERFS_I(inode)->i_flags & ~i_has_xattr_dir; |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame^] | 455 | else |
| 456 | reiserfs_warning(inode->i_sb, "jdm-20004", |
| 457 | "Couldn't remove all xattrs (%d)\n", err); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 458 | return err; |
| 459 | } |
| 460 | |
| 461 | struct reiserfs_chown_buf { |
| 462 | struct inode *inode; |
| 463 | struct dentry *xadir; |
| 464 | struct iattr *attrs; |
| 465 | }; |
| 466 | |
| 467 | /* XXX: If there is a better way to do this, I'd love to hear about it */ |
| 468 | static int |
| 469 | reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen, |
| 470 | loff_t offset, u64 ino, unsigned int d_type) |
| 471 | { |
| 472 | struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf; |
| 473 | struct dentry *xafile, *xadir = chown_buf->xadir; |
| 474 | struct iattr *attrs = chown_buf->attrs; |
| 475 | int err = 0; |
| 476 | |
| 477 | xafile = lookup_one_len(name, xadir, namelen); |
| 478 | if (IS_ERR(xafile)) |
| 479 | return PTR_ERR(xafile); |
| 480 | else if (!xafile->d_inode) { |
| 481 | dput(xafile); |
| 482 | return -ENODATA; |
| 483 | } |
| 484 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 485 | if (!S_ISDIR(xafile->d_inode->i_mode)) { |
| 486 | mutex_lock_nested(&xafile->d_inode->i_mutex, I_MUTEX_CHILD); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 487 | err = notify_change(xafile, attrs); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 488 | mutex_unlock(&xafile->d_inode->i_mutex); |
| 489 | } |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 490 | dput(xafile); |
| 491 | |
| 492 | return err; |
| 493 | } |
| 494 | |
| 495 | int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs) |
| 496 | { |
| 497 | struct dentry *dir; |
| 498 | int err = 0; |
| 499 | struct reiserfs_chown_buf buf; |
| 500 | unsigned int ia_valid = attrs->ia_valid; |
| 501 | |
| 502 | /* Skip out, an xattr has no xattrs associated with it */ |
| 503 | if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1) |
| 504 | return 0; |
| 505 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 506 | dir = open_xa_dir(inode, XATTR_REPLACE); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 507 | if (IS_ERR(dir)) { |
| 508 | if (PTR_ERR(dir) != -ENODATA) |
| 509 | err = PTR_ERR(dir); |
| 510 | goto out; |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 511 | } else if (!dir->d_inode) |
| 512 | goto out_dir; |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 513 | |
| 514 | attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME); |
| 515 | buf.xadir = dir; |
| 516 | buf.attrs = attrs; |
| 517 | buf.inode = inode; |
| 518 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 519 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 520 | err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 521 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 522 | if (!err) |
| 523 | err = notify_change(dir, attrs); |
| 524 | mutex_unlock(&dir->d_inode->i_mutex); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 525 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 526 | attrs->ia_valid = ia_valid; |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 527 | out_dir: |
| 528 | dput(dir); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 529 | out: |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 530 | return err; |
| 531 | } |
| 532 | |
| 533 | #ifdef CONFIG_REISERFS_FS_XATTR |
| 534 | static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char |
| 535 | *prefix); |
| 536 | |
| 537 | /* Returns a dentry corresponding to a specific extended attribute file |
| 538 | * for the inode. If flags allow, the file is created. Otherwise, a |
| 539 | * valid or negative dentry, or an error is returned. */ |
| 540 | static struct dentry *get_xa_file_dentry(const struct inode *inode, |
| 541 | const char *name, int flags) |
| 542 | { |
| 543 | struct dentry *xadir, *xafile; |
| 544 | int err = 0; |
| 545 | |
| 546 | xadir = open_xa_dir(inode, flags); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 547 | if (IS_ERR(xadir)) |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 548 | return ERR_CAST(xadir); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 549 | |
| 550 | xafile = lookup_one_len(name, xadir, strlen(name)); |
| 551 | if (IS_ERR(xafile)) { |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 552 | err = PTR_ERR(xafile); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 553 | goto out; |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 554 | } |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 555 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 556 | if (xafile->d_inode && (flags & XATTR_CREATE)) |
| 557 | err = -EEXIST; |
| 558 | |
| 559 | if (!xafile->d_inode) { |
| 560 | err = -ENODATA; |
| 561 | if (xattr_may_create(flags)) { |
| 562 | mutex_lock_nested(&xadir->d_inode->i_mutex, |
| 563 | I_MUTEX_XATTR); |
| 564 | err = xattr_create(xadir->d_inode, xafile, |
| 565 | 0700|S_IFREG); |
| 566 | mutex_unlock(&xadir->d_inode->i_mutex); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 570 | if (err) |
| 571 | dput(xafile); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 572 | out: |
| 573 | dput(xadir); |
| 574 | if (err) |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 575 | return ERR_PTR(err); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 576 | return xafile; |
| 577 | } |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* Internal operations on file data */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 580 | static inline void reiserfs_put_page(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 582 | kunmap(page); |
| 583 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Jeff Mahoney | ec6ea56 | 2009-03-30 14:02:30 -0400 | [diff] [blame] | 586 | static struct page *reiserfs_get_page(struct inode *dir, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 588 | struct address_space *mapping = dir->i_mapping; |
| 589 | struct page *page; |
| 590 | /* We can deadlock if we try to free dentries, |
| 591 | and an unlink/rmdir has just occured - GFP_NOFS avoids this */ |
Al Viro | c4cdd03 | 2005-10-21 03:22:39 -0400 | [diff] [blame] | 592 | mapping_set_gfp_mask(mapping, GFP_NOFS); |
Jeff Mahoney | ec6ea56 | 2009-03-30 14:02:30 -0400 | [diff] [blame] | 593 | page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 594 | if (!IS_ERR(page)) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 595 | kmap(page); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 596 | if (PageError(page)) |
| 597 | goto fail; |
| 598 | } |
| 599 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 601 | fail: |
| 602 | reiserfs_put_page(page); |
| 603 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 606 | static inline __u32 xattr_hash(const char *msg, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 608 | return csum_partial(msg, len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 611 | int reiserfs_commit_write(struct file *f, struct page *page, |
| 612 | unsigned from, unsigned to); |
| 613 | int reiserfs_prepare_write(struct file *f, struct page *page, |
| 614 | unsigned from, unsigned to); |
| 615 | |
| 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | /* Generic extended attribute operations that can be used by xa plugins */ |
| 618 | |
| 619 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 620 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | */ |
| 622 | int |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 623 | reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer, |
| 624 | size_t buffer_size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 626 | int err = 0; |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 627 | struct dentry *dentry; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 628 | struct page *page; |
| 629 | char *data; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 630 | size_t file_pos = 0; |
| 631 | size_t buffer_pos = 0; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 632 | struct iattr newattrs; |
| 633 | __u32 xahash = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 635 | if (get_inode_sd_version(inode) == STAT_DATA_V1) |
| 636 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 638 | /* Empty xattrs are ok, they're just empty files, no hash */ |
| 639 | if (buffer && buffer_size) |
| 640 | xahash = xattr_hash(buffer, buffer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 642 | dentry = get_xa_file_dentry(inode, name, flags); |
| 643 | if (IS_ERR(dentry)) { |
| 644 | err = PTR_ERR(dentry); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 645 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 648 | REISERFS_I(inode)->i_flags |= i_has_xattr_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 650 | /* Resize it so we're ok to write there */ |
| 651 | newattrs.ia_size = buffer_size; |
| 652 | newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME; |
Jeff Mahoney | f437c52 | 2009-03-30 14:02:29 -0400 | [diff] [blame] | 653 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR); |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 654 | err = notify_change(dentry, &newattrs); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 655 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 656 | if (err) |
| 657 | goto out_filp; |
| 658 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 659 | while (buffer_pos < buffer_size || buffer_pos == 0) { |
| 660 | size_t chunk; |
| 661 | size_t skip = 0; |
| 662 | size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1)); |
| 663 | if (buffer_size - buffer_pos > PAGE_CACHE_SIZE) |
| 664 | chunk = PAGE_CACHE_SIZE; |
| 665 | else |
| 666 | chunk = buffer_size - buffer_pos; |
| 667 | |
Jeff Mahoney | ec6ea56 | 2009-03-30 14:02:30 -0400 | [diff] [blame] | 668 | page = reiserfs_get_page(dentry->d_inode, file_pos); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 669 | if (IS_ERR(page)) { |
| 670 | err = PTR_ERR(page); |
| 671 | goto out_filp; |
| 672 | } |
| 673 | |
| 674 | lock_page(page); |
| 675 | data = page_address(page); |
| 676 | |
| 677 | if (file_pos == 0) { |
| 678 | struct reiserfs_xattr_header *rxh; |
| 679 | skip = file_pos = sizeof(struct reiserfs_xattr_header); |
| 680 | if (chunk + skip > PAGE_CACHE_SIZE) |
| 681 | chunk = PAGE_CACHE_SIZE - skip; |
| 682 | rxh = (struct reiserfs_xattr_header *)data; |
| 683 | rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC); |
| 684 | rxh->h_hash = cpu_to_le32(xahash); |
| 685 | } |
| 686 | |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 687 | err = reiserfs_prepare_write(NULL, page, page_offset, |
Vladimir Saveliev | ba9d8ce | 2007-10-16 01:25:14 -0700 | [diff] [blame] | 688 | page_offset + chunk + skip); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 689 | if (!err) { |
| 690 | if (buffer) |
| 691 | memcpy(data + skip, buffer + buffer_pos, chunk); |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 692 | err = reiserfs_commit_write(NULL, page, page_offset, |
| 693 | page_offset + chunk + |
| 694 | skip); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 695 | } |
| 696 | unlock_page(page); |
| 697 | reiserfs_put_page(page); |
| 698 | buffer_pos += chunk; |
| 699 | file_pos += chunk; |
| 700 | skip = 0; |
| 701 | if (err || buffer_size == 0 || !buffer) |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | /* We can't mark the inode dirty if it's not hashed. This is the case |
| 706 | * when we're inheriting the default ACL. If we dirty it, the inode |
| 707 | * gets marked dirty, but won't (ever) make it onto the dirty list until |
| 708 | * it's synced explicitly to clear I_DIRTY. This is bad. */ |
| 709 | if (!hlist_unhashed(&inode->i_hash)) { |
| 710 | inode->i_ctime = CURRENT_TIME_SEC; |
| 711 | mark_inode_dirty(inode); |
| 712 | } |
| 713 | |
| 714 | out_filp: |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 715 | dput(dentry); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 716 | |
| 717 | out: |
| 718 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 722 | * inode->i_mutex: down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | */ |
| 724 | int |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 725 | reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer, |
| 726 | size_t buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 728 | ssize_t err = 0; |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 729 | struct dentry *dentry; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 730 | size_t isize; |
| 731 | size_t file_pos = 0; |
| 732 | size_t buffer_pos = 0; |
| 733 | struct page *page; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 734 | __u32 hash = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 736 | if (name == NULL) |
| 737 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 739 | /* We can't have xattrs attached to v1 items since they don't have |
| 740 | * generation numbers */ |
| 741 | if (get_inode_sd_version(inode) == STAT_DATA_V1) |
| 742 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 744 | dentry = get_xa_file_dentry(inode, name, XATTR_REPLACE); |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 745 | if (IS_ERR(dentry)) { |
| 746 | err = PTR_ERR(dentry); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 747 | goto out; |
| 748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame^] | 750 | /* protect against concurrent access. xattrs are backed by |
| 751 | * regular files, but they're not regular files. The updates |
| 752 | * must be atomic from the perspective of the user. */ |
| 753 | mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR); |
| 754 | |
Jeff Mahoney | f437c52 | 2009-03-30 14:02:29 -0400 | [diff] [blame] | 755 | isize = i_size_read(dentry->d_inode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 756 | REISERFS_I(inode)->i_flags |= i_has_xattr_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 758 | /* Just return the size needed */ |
| 759 | if (buffer == NULL) { |
| 760 | err = isize - sizeof(struct reiserfs_xattr_header); |
| 761 | goto out_dput; |
| 762 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 764 | if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) { |
| 765 | err = -ERANGE; |
| 766 | goto out_dput; |
| 767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 769 | while (file_pos < isize) { |
| 770 | size_t chunk; |
| 771 | char *data; |
| 772 | size_t skip = 0; |
| 773 | if (isize - file_pos > PAGE_CACHE_SIZE) |
| 774 | chunk = PAGE_CACHE_SIZE; |
| 775 | else |
| 776 | chunk = isize - file_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Jeff Mahoney | ec6ea56 | 2009-03-30 14:02:30 -0400 | [diff] [blame] | 778 | page = reiserfs_get_page(dentry->d_inode, file_pos); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 779 | if (IS_ERR(page)) { |
| 780 | err = PTR_ERR(page); |
| 781 | goto out_dput; |
| 782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 784 | lock_page(page); |
| 785 | data = page_address(page); |
| 786 | if (file_pos == 0) { |
| 787 | struct reiserfs_xattr_header *rxh = |
| 788 | (struct reiserfs_xattr_header *)data; |
| 789 | skip = file_pos = sizeof(struct reiserfs_xattr_header); |
| 790 | chunk -= skip; |
| 791 | /* Magic doesn't match up.. */ |
| 792 | if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) { |
| 793 | unlock_page(page); |
| 794 | reiserfs_put_page(page); |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 795 | reiserfs_warning(inode->i_sb, "jdm-20001", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 796 | "Invalid magic for xattr (%s) " |
| 797 | "associated with %k", name, |
| 798 | INODE_PKEY(inode)); |
| 799 | err = -EIO; |
| 800 | goto out_dput; |
| 801 | } |
| 802 | hash = le32_to_cpu(rxh->h_hash); |
| 803 | } |
| 804 | memcpy(buffer + buffer_pos, data + skip, chunk); |
| 805 | unlock_page(page); |
| 806 | reiserfs_put_page(page); |
| 807 | file_pos += chunk; |
| 808 | buffer_pos += chunk; |
| 809 | skip = 0; |
| 810 | } |
| 811 | err = isize - sizeof(struct reiserfs_xattr_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 813 | if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) != |
| 814 | hash) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 815 | reiserfs_warning(inode->i_sb, "jdm-20002", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 816 | "Invalid hash for xattr (%s) associated " |
| 817 | "with %k", name, INODE_PKEY(inode)); |
| 818 | err = -EIO; |
| 819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 821 | out_dput: |
Jeff Mahoney | d984561 | 2009-03-30 14:02:35 -0400 | [diff] [blame^] | 822 | mutex_unlock(&dentry->d_inode->i_mutex); |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 823 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 825 | out: |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 826 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 829 | int reiserfs_xattr_del(struct inode *inode, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 831 | struct dentry *dir; |
| 832 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 834 | dir = open_xa_dir(inode, XATTR_REPLACE); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 835 | if (IS_ERR(dir)) { |
| 836 | err = PTR_ERR(dir); |
| 837 | goto out; |
| 838 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 840 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 841 | err = __reiserfs_xattr_del(dir, name, strlen(name)); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 842 | mutex_unlock(&dir->d_inode->i_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 843 | dput(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 845 | if (!err) { |
| 846 | inode->i_ctime = CURRENT_TIME_SEC; |
| 847 | mark_inode_dirty(inode); |
| 848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 850 | out: |
| 851 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | /* Actual operations that are exported to VFS-land */ |
| 855 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 856 | static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | /* |
| 858 | * Inode operation getxattr() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | */ |
| 860 | ssize_t |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 861 | reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer, |
| 862 | size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 864 | struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name); |
| 865 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 867 | if (!xah || !reiserfs_xattrs(dentry->d_sb) || |
| 868 | get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) |
| 869 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 871 | reiserfs_read_lock_xattr_i(dentry->d_inode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 872 | err = xah->get(dentry->d_inode, name, buffer, size); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 873 | reiserfs_read_unlock_xattr_i(dentry->d_inode); |
| 874 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | /* |
| 878 | * Inode operation setxattr() |
| 879 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 880 | * dentry->d_inode->i_mutex down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | */ |
| 882 | int |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 883 | reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
| 884 | size_t size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 886 | struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name); |
| 887 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 889 | if (!xah || !reiserfs_xattrs(dentry->d_sb) || |
| 890 | get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) |
| 891 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 893 | reiserfs_write_lock_xattr_i(dentry->d_inode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 894 | err = xah->set(dentry->d_inode, name, value, size, flags); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 895 | reiserfs_write_unlock_xattr_i(dentry->d_inode); |
| 896 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /* |
| 900 | * Inode operation removexattr() |
| 901 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 902 | * dentry->d_inode->i_mutex down |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 904 | int reiserfs_removexattr(struct dentry *dentry, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 906 | int err; |
| 907 | struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 909 | if (!xah || !reiserfs_xattrs(dentry->d_sb) || |
| 910 | get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) |
| 911 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 913 | reiserfs_write_lock_xattr_i(dentry->d_inode); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 914 | /* Deletion pre-operation */ |
| 915 | if (xah->del) { |
| 916 | err = xah->del(dentry->d_inode, name); |
| 917 | if (err) |
| 918 | goto out; |
| 919 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 921 | err = reiserfs_xattr_del(dentry->d_inode, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 923 | dentry->d_inode->i_ctime = CURRENT_TIME_SEC; |
| 924 | mark_inode_dirty(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 926 | out: |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 927 | reiserfs_write_unlock_xattr_i(dentry->d_inode); |
| 928 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | /* This is what filldir will use: |
| 932 | * r_pos will always contain the amount of space required for the entire |
| 933 | * list. If r_pos becomes larger than r_size, we need more space and we |
| 934 | * return an error indicating this. If r_pos is less than r_size, then we've |
| 935 | * filled the buffer successfully and we return success */ |
| 936 | struct reiserfs_listxattr_buf { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 937 | int r_pos; |
| 938 | int r_size; |
| 939 | char *r_buf; |
| 940 | struct inode *r_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | }; |
| 942 | |
| 943 | static int |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 944 | reiserfs_listxattr_filler(void *buf, const char *name, int namelen, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 945 | loff_t offset, u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 947 | struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf; |
| 948 | int len = 0; |
| 949 | if (name[0] != '.' |
| 950 | || (namelen != 1 && (name[1] != '.' || namelen != 2))) { |
| 951 | struct reiserfs_xattr_handler *xah = |
| 952 | find_xattr_handler_prefix(name); |
| 953 | if (!xah) |
| 954 | return 0; /* Unsupported xattr name, skip it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 956 | /* We call ->list() twice because the operation isn't required to just |
| 957 | * return the name back - we want to make sure we have enough space */ |
| 958 | len += xah->list(b->r_inode, name, namelen, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 960 | if (len) { |
| 961 | if (b->r_pos + len + 1 <= b->r_size) { |
| 962 | char *p = b->r_buf + b->r_pos; |
| 963 | p += xah->list(b->r_inode, name, namelen, p); |
| 964 | *p++ = '\0'; |
| 965 | } |
| 966 | b->r_pos += len + 1; |
| 967 | } |
| 968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 970 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | /* |
| 974 | * Inode operation listxattr() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 976 | ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 978 | struct dentry *dir; |
| 979 | int err = 0; |
| 980 | struct reiserfs_listxattr_buf buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 982 | if (!dentry->d_inode) |
| 983 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 985 | if (!reiserfs_xattrs(dentry->d_sb) || |
| 986 | get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1) |
| 987 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 989 | reiserfs_read_lock_xattr_i(dentry->d_inode); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 990 | dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 991 | if (IS_ERR(dir)) { |
| 992 | err = PTR_ERR(dir); |
| 993 | if (err == -ENODATA) |
| 994 | err = 0; /* Not an error if there aren't any xattrs */ |
| 995 | goto out; |
| 996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 998 | buf.r_buf = buffer; |
| 999 | buf.r_size = buffer ? size : 0; |
| 1000 | buf.r_pos = 0; |
| 1001 | buf.r_inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1003 | REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 1005 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR); |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 1006 | err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 1007 | mutex_unlock(&dir->d_inode->i_mutex); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1008 | if (err) |
| 1009 | goto out_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1011 | if (buf.r_pos > buf.r_size && buffer != NULL) |
| 1012 | err = -ERANGE; |
| 1013 | else |
| 1014 | err = buf.r_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1016 | out_dir: |
Jeff Mahoney | 3227e14 | 2008-02-15 14:37:22 -0800 | [diff] [blame] | 1017 | dput(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1019 | out: |
| 1020 | reiserfs_read_unlock_xattr_i(dentry->d_inode); |
| 1021 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /* This is the implementation for the xattr plugin infrastructure */ |
Denis Cheng | bcf11cb | 2008-02-06 01:37:40 -0800 | [diff] [blame] | 1025 | static LIST_HEAD(xattr_handlers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | static DEFINE_RWLOCK(handler_lock); |
| 1027 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1028 | static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char |
| 1029 | *prefix) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1031 | struct reiserfs_xattr_handler *xah = NULL; |
| 1032 | struct list_head *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1034 | read_lock(&handler_lock); |
| 1035 | list_for_each(p, &xattr_handlers) { |
| 1036 | xah = list_entry(p, struct reiserfs_xattr_handler, handlers); |
| 1037 | if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0) |
| 1038 | break; |
| 1039 | xah = NULL; |
| 1040 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1042 | read_unlock(&handler_lock); |
| 1043 | return xah; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1046 | static void __unregister_handlers(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1048 | struct reiserfs_xattr_handler *xah; |
| 1049 | struct list_head *p, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1051 | list_for_each_safe(p, tmp, &xattr_handlers) { |
| 1052 | xah = list_entry(p, struct reiserfs_xattr_handler, handlers); |
| 1053 | if (xah->exit) |
| 1054 | xah->exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1056 | list_del_init(p); |
| 1057 | } |
| 1058 | INIT_LIST_HEAD(&xattr_handlers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1061 | int __init reiserfs_xattr_register_handlers(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1063 | int err = 0; |
| 1064 | struct reiserfs_xattr_handler *xah; |
| 1065 | struct list_head *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1067 | write_lock(&handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1069 | /* If we're already initialized, nothing to do */ |
| 1070 | if (!list_empty(&xattr_handlers)) { |
| 1071 | write_unlock(&handler_lock); |
| 1072 | return 0; |
| 1073 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1075 | /* Add the handlers */ |
| 1076 | list_add_tail(&user_handler.handlers, &xattr_handlers); |
| 1077 | list_add_tail(&trusted_handler.handlers, &xattr_handlers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | #ifdef CONFIG_REISERFS_FS_SECURITY |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1079 | list_add_tail(&security_handler.handlers, &xattr_handlers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | #endif |
| 1081 | #ifdef CONFIG_REISERFS_FS_POSIX_ACL |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1082 | list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers); |
| 1083 | list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | #endif |
| 1085 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1086 | /* Run initializers, if available */ |
| 1087 | list_for_each(p, &xattr_handlers) { |
| 1088 | xah = list_entry(p, struct reiserfs_xattr_handler, handlers); |
| 1089 | if (xah->init) { |
| 1090 | err = xah->init(); |
| 1091 | if (err) { |
| 1092 | list_del_init(p); |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1098 | /* Clean up other handlers, if any failed */ |
| 1099 | if (err) |
| 1100 | __unregister_handlers(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1102 | write_unlock(&handler_lock); |
| 1103 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1106 | void reiserfs_xattr_unregister_handlers(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1108 | write_lock(&handler_lock); |
| 1109 | __unregister_handlers(); |
| 1110 | write_unlock(&handler_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1113 | static int reiserfs_check_acl(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | { |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1115 | struct posix_acl *acl; |
| 1116 | int error = -EAGAIN; /* do regular unix permission checks by default */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1118 | reiserfs_read_lock_xattr_i(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1120 | acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS); |
| 1121 | |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1122 | reiserfs_read_unlock_xattr_i(inode); |
| 1123 | |
| 1124 | if (acl) { |
| 1125 | if (!IS_ERR(acl)) { |
| 1126 | error = posix_acl_permission(inode, acl, mask); |
| 1127 | posix_acl_release(acl); |
| 1128 | } else if (PTR_ERR(acl) != -ENODATA) |
| 1129 | error = PTR_ERR(acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1132 | return error; |
| 1133 | } |
| 1134 | |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 1135 | int reiserfs_permission(struct inode *inode, int mask) |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1136 | { |
| 1137 | /* |
| 1138 | * We don't do permission checks on the internal objects. |
| 1139 | * Permissions are determined by the "owning" object. |
| 1140 | */ |
Jeff Mahoney | 6dfede69 | 2009-03-30 14:02:32 -0400 | [diff] [blame] | 1141 | if (IS_PRIVATE(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | /* |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1144 | * Stat data v1 doesn't support ACLs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | */ |
Christoph Hellwig | ec19157 | 2006-02-01 03:06:46 -0800 | [diff] [blame] | 1146 | if (get_inode_sd_version(inode) == STAT_DATA_V1) |
| 1147 | return generic_permission(inode, mask, NULL); |
| 1148 | else |
| 1149 | return generic_permission(inode, mask, reiserfs_check_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | } |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 1151 | |
| 1152 | static int create_privroot(struct dentry *dentry) |
| 1153 | { |
| 1154 | int err; |
| 1155 | struct inode *inode = dentry->d_parent->d_inode; |
| 1156 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR); |
Jeff Mahoney | 6c17675 | 2009-03-30 14:02:34 -0400 | [diff] [blame] | 1157 | err = xattr_mkdir(inode, dentry, 0700); |
Jeff Mahoney | a72bdb1 | 2009-03-30 14:02:33 -0400 | [diff] [blame] | 1158 | mutex_unlock(&inode->i_mutex); |
| 1159 | if (err) { |
| 1160 | dput(dentry); |
| 1161 | dentry = NULL; |
| 1162 | } |
| 1163 | |
| 1164 | if (dentry && dentry->d_inode) |
| 1165 | reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr " |
| 1166 | "storage.\n", PRIVROOT_NAME); |
| 1167 | |
| 1168 | return err; |
| 1169 | } |
| 1170 | |
| 1171 | static int xattr_mount_check(struct super_block *s) |
| 1172 | { |
| 1173 | /* We need generation numbers to ensure that the oid mapping is correct |
| 1174 | * v3.5 filesystems don't have them. */ |
| 1175 | if (!old_format_only(s)) { |
| 1176 | set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt)); |
| 1177 | } else if (reiserfs_xattrs_optional(s)) { |
| 1178 | /* Old format filesystem, but optional xattrs have been enabled |
| 1179 | * at mount time. Error out. */ |
| 1180 | reiserfs_warning(s, "jdm-20005", |
| 1181 | "xattrs/ACLs not supported on pre v3.6 " |
| 1182 | "format filesystem. Failing mount."); |
| 1183 | return -EOPNOTSUPP; |
| 1184 | } else { |
| 1185 | /* Old format filesystem, but no optional xattrs have |
| 1186 | * been enabled. This means we silently disable xattrs |
| 1187 | * on the filesystem. */ |
| 1188 | clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt)); |
| 1189 | } |
| 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | #else |
| 1195 | int __init reiserfs_xattr_register_handlers(void) { return 0; } |
| 1196 | void reiserfs_xattr_unregister_handlers(void) {} |
| 1197 | #endif |
| 1198 | |
| 1199 | /* This will catch lookups from the fs root to .reiserfs_priv */ |
| 1200 | static int |
| 1201 | xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name) |
| 1202 | { |
| 1203 | struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root; |
| 1204 | if (name->len == priv_root->d_name.len && |
| 1205 | name->hash == priv_root->d_name.hash && |
| 1206 | !memcmp(name->name, priv_root->d_name.name, name->len)) { |
| 1207 | return -ENOENT; |
| 1208 | } else if (q1->len == name->len && |
| 1209 | !memcmp(q1->name, name->name, name->len)) |
| 1210 | return 0; |
| 1211 | return 1; |
| 1212 | } |
| 1213 | |
| 1214 | static struct dentry_operations xattr_lookup_poison_ops = { |
| 1215 | .d_compare = xattr_lookup_poison, |
| 1216 | }; |
| 1217 | |
| 1218 | /* We need to take a copy of the mount flags since things like |
| 1219 | * MS_RDONLY don't get set until *after* we're called. |
| 1220 | * mount_flags != mount_options */ |
| 1221 | int reiserfs_xattr_init(struct super_block *s, int mount_flags) |
| 1222 | { |
| 1223 | int err = 0; |
| 1224 | |
| 1225 | #ifdef CONFIG_REISERFS_FS_XATTR |
| 1226 | err = xattr_mount_check(s); |
| 1227 | if (err) |
| 1228 | goto error; |
| 1229 | #endif |
| 1230 | |
| 1231 | /* If we don't have the privroot located yet - go find it */ |
| 1232 | if (!REISERFS_SB(s)->priv_root) { |
| 1233 | struct dentry *dentry; |
| 1234 | dentry = lookup_one_len(PRIVROOT_NAME, s->s_root, |
| 1235 | strlen(PRIVROOT_NAME)); |
| 1236 | if (!IS_ERR(dentry)) { |
| 1237 | #ifdef CONFIG_REISERFS_FS_XATTR |
| 1238 | if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) |
| 1239 | err = create_privroot(dentry); |
| 1240 | #endif |
| 1241 | if (!dentry->d_inode) { |
| 1242 | dput(dentry); |
| 1243 | dentry = NULL; |
| 1244 | } |
| 1245 | } else |
| 1246 | err = PTR_ERR(dentry); |
| 1247 | |
| 1248 | if (!err && dentry) { |
| 1249 | s->s_root->d_op = &xattr_lookup_poison_ops; |
| 1250 | dentry->d_inode->i_flags |= S_PRIVATE; |
| 1251 | REISERFS_SB(s)->priv_root = dentry; |
| 1252 | #ifdef CONFIG_REISERFS_FS_XATTR |
| 1253 | /* xattrs are unavailable */ |
| 1254 | } else if (!(mount_flags & MS_RDONLY)) { |
| 1255 | /* If we're read-only it just means that the dir |
| 1256 | * hasn't been created. Not an error -- just no |
| 1257 | * xattrs on the fs. We'll check again if we |
| 1258 | * go read-write */ |
| 1259 | reiserfs_warning(s, "jdm-20006", |
| 1260 | "xattrs/ACLs enabled and couldn't " |
| 1261 | "find/create .reiserfs_priv. " |
| 1262 | "Failing mount."); |
| 1263 | err = -EOPNOTSUPP; |
| 1264 | #endif |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | #ifdef CONFIG_REISERFS_FS_XATTR |
| 1269 | error: |
| 1270 | if (err) { |
| 1271 | clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt)); |
| 1272 | clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt)); |
| 1273 | clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt)); |
| 1274 | } |
| 1275 | #endif |
| 1276 | |
| 1277 | /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */ |
| 1278 | s->s_flags = s->s_flags & ~MS_POSIXACL; |
| 1279 | #ifdef CONFIG_REISERFS_FS_POSIX_ACL |
| 1280 | if (reiserfs_posixacl(s)) |
| 1281 | s->s_flags |= MS_POSIXACL; |
| 1282 | #endif |
| 1283 | |
| 1284 | return err; |
| 1285 | } |