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