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