Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | /* File: fs/xattr.c Extended attribute handling. Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org> Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com> */ #include <linux/fs.h> #include <linux/slab.h> #include <linux/vmalloc.h> #include <linux/smp_lock.h> #include <linux/file.h> #include <linux/xattr.h> #include <asm/uaccess.h> /* * Extended attribute memory allocation wrappers, originally * based on the Intermezzo PRESTO_ALLOC/PRESTO_FREE macros. * The vmalloc use here is very uncommon - extended attributes * are supposed to be small chunks of metadata, and it is quite * unusual to have very many extended attributes, so lists tend * to be quite short as well. The 64K upper limit is derived * from the extended attribute size limit used by XFS. * Intentionally allow zero @size for value/list size requests. */ static void * xattr_alloc(size_t size, size_t limit) { void *ptr; if (size > limit) return ERR_PTR(-E2BIG); if (!size) /* size request, no buffer is needed */ return NULL; else if (size <= PAGE_SIZE) ptr = kmalloc((unsigned long) size, GFP_KERNEL); else ptr = vmalloc((unsigned long) size); if (!ptr) return ERR_PTR(-ENOMEM); return ptr; } static void xattr_free(void *ptr, size_t size) { if (!size) /* size request, no buffer was needed */ return; else if (size <= PAGE_SIZE) kfree(ptr); else vfree(ptr); } /* * Extended attribute SET operations */ static long setxattr(struct dentry *d, char *name, void *value, size_t size, int flags) { int error; void *kvalue; char kname[XATTR_NAME_MAX + 1]; if (flags & ~(XATTR_CREATE|XATTR_REPLACE)) return -EINVAL; if (copy_from_user(kname, name, XATTR_NAME_MAX)) return -EFAULT; kname[XATTR_NAME_MAX] = '\0'; kvalue = xattr_alloc(size, XATTR_SIZE_MAX); if (IS_ERR(kvalue)) return PTR_ERR(kvalue); if (size > 0 && copy_from_user(kvalue, value, size)) { xattr_free(kvalue, size); return -EFAULT; } error = -EOPNOTSUPP; if (d->d_inode->i_op && d->d_inode->i_op->setxattr) { lock_kernel(); error = d->d_inode->i_op->setxattr(d, kname, kvalue, size, flags); unlock_kernel(); } xattr_free(kvalue, size); return error; } asmlinkage long sys_setxattr(char *path, char *name, void *value, size_t size, int flags) { struct nameidata nd; int error; error = user_path_walk(path, &nd); if (error) return error; error = setxattr(nd.dentry, name, value, size, flags); path_release(&nd); return error; } asmlinkage long sys_lsetxattr(char *path, char *name, void *value, size_t size, int flags) { struct nameidata nd; int error; error = user_path_walk_link(path, &nd); if (error) return error; error = setxattr(nd.dentry, name, value, size, flags); path_release(&nd); return error; } asmlinkage long sys_fsetxattr(int fd, char *name, void *value, size_t size, int flags) { struct file *f; int error = -EBADF; f = fget(fd); if (!f) return error; error = setxattr(f->f_dentry, name, value, size, flags); fput(f); return error; } /* * Extended attribute GET operations */ static long getxattr(struct dentry *d, char *name, void *value, size_t size) { int error; void *kvalue; char kname[XATTR_NAME_MAX + 1]; if (copy_from_user(kname, name, XATTR_NAME_MAX)) return -EFAULT; kname[XATTR_NAME_MAX] = '\0'; kvalue = xattr_alloc(size, XATTR_SIZE_MAX); if (IS_ERR(kvalue)) return PTR_ERR(kvalue); error = -EOPNOTSUPP; if (d->d_inode->i_op && d->d_inode->i_op->getxattr) { lock_kernel(); error = d->d_inode->i_op->getxattr(d, kname, kvalue, size); unlock_kernel(); } if (kvalue && error > 0) if (copy_to_user(value, kvalue, size)) error = -EFAULT; xattr_free(kvalue, size); return error; } asmlinkage long sys_getxattr(char *path, char *name, void *value, size_t size) { struct nameidata nd; int error; error = user_path_walk(path, &nd); if (error) return error; error = getxattr(nd.dentry, name, value, size); path_release(&nd); return error; } asmlinkage long sys_lgetxattr(char *path, char *name, void *value, size_t size) { struct nameidata nd; int error; error = user_path_walk_link(path, &nd); if (error) return error; error = getxattr(nd.dentry, name, value, size); path_release(&nd); return error; } asmlinkage long sys_fgetxattr(int fd, char *name, void *value, size_t size) { struct file *f; int error = -EBADF; f = fget(fd); if (!f) return error; error = getxattr(f->f_dentry, name, value, size); fput(f); return error; } /* * Extended attribute LIST operations */ static long listxattr(struct dentry *d, char *list, size_t size) { int error; char *klist; klist = (char *)xattr_alloc(size, XATTR_LIST_MAX); if (IS_ERR(klist)) return PTR_ERR(klist); error = -EOPNOTSUPP; if (d->d_inode->i_op && d->d_inode->i_op->listxattr) { lock_kernel(); error = d->d_inode->i_op->listxattr(d, klist, size); unlock_kernel(); } if (klist && error > 0) if (copy_to_user(list, klist, size)) error = -EFAULT; xattr_free(klist, size); return error; } asmlinkage long sys_listxattr(char *path, char *list, size_t size) { struct nameidata nd; int error; error = user_path_walk(path, &nd); if (error) return error; error = listxattr(nd.dentry, list, size); path_release(&nd); return error; } asmlinkage long sys_llistxattr(char *path, char *list, size_t size) { struct nameidata nd; int error; error = user_path_walk_link(path, &nd); if (error) return error; error = listxattr(nd.dentry, list, size); path_release(&nd); return error; } asmlinkage long sys_flistxattr(int fd, char *list, size_t size) { struct file *f; int error = -EBADF; f = fget(fd); if (!f) return error; error = listxattr(f->f_dentry, list, size); fput(f); return error; } /* * Extended attribute REMOVE operations */ static long removexattr(struct dentry *d, char *name) { int error; char kname[XATTR_NAME_MAX + 1]; if (copy_from_user(kname, name, XATTR_NAME_MAX)) return -EFAULT; kname[XATTR_NAME_MAX] = '\0'; error = -EOPNOTSUPP; if (d->d_inode->i_op && d->d_inode->i_op->removexattr) { lock_kernel(); error = d->d_inode->i_op->removexattr(d, kname); unlock_kernel(); } return error; } asmlinkage long sys_removexattr(char *path, char *name) { struct nameidata nd; int error; error = user_path_walk(path, &nd); if (error) return error; error = removexattr(nd.dentry, name); path_release(&nd); return error; } asmlinkage long sys_lremovexattr(char *path, char *name) { struct nameidata nd; int error; error = user_path_walk_link(path, &nd); if (error) return error; error = removexattr(nd.dentry, name); path_release(&nd); return error; } asmlinkage long sys_fremovexattr(int fd, char *name) { struct file *f; int error = -EBADF; f = fget(fd); if (!f) return error; error = removexattr(f->f_dentry, name); fput(f); return error; } |