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