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