|
8 | 8 | #include <linux/cred.h> |
9 | 9 | #include <linux/buffer_head.h> |
10 | 10 | #include <linux/blkdev.h> |
| 11 | +#include <linux/fsnotify.h> |
| 12 | +#include <linux/security.h> |
| 13 | +#include <linux/msdos_fs.h> |
11 | 14 |
|
12 | 15 | #include "exfat_raw.h" |
13 | 16 | #include "exfat_fs.h" |
@@ -144,7 +147,7 @@ int __exfat_truncate(struct inode *inode) |
144 | 147 | } |
145 | 148 |
|
146 | 149 | if (ei->type == TYPE_FILE) |
147 | | - ei->attr |= ATTR_ARCHIVE; |
| 150 | + ei->attr |= EXFAT_ATTR_ARCHIVE; |
148 | 151 |
|
149 | 152 | /* |
150 | 153 | * update the directory entry |
@@ -315,6 +318,93 @@ int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
315 | 318 | return error; |
316 | 319 | } |
317 | 320 |
|
| 321 | +/* |
| 322 | + * modified ioctls from fat/file.c by Welmer Almesberger |
| 323 | + */ |
| 324 | +static int exfat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr) |
| 325 | +{ |
| 326 | + u32 attr; |
| 327 | + |
| 328 | + inode_lock_shared(inode); |
| 329 | + attr = exfat_make_attr(inode); |
| 330 | + inode_unlock_shared(inode); |
| 331 | + |
| 332 | + return put_user(attr, user_attr); |
| 333 | +} |
| 334 | + |
| 335 | +static int exfat_ioctl_set_attributes(struct file *file, u32 __user *user_attr) |
| 336 | +{ |
| 337 | + struct inode *inode = file_inode(file); |
| 338 | + struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); |
| 339 | + int is_dir = S_ISDIR(inode->i_mode); |
| 340 | + u32 attr, oldattr; |
| 341 | + struct iattr ia; |
| 342 | + int err; |
| 343 | + |
| 344 | + err = get_user(attr, user_attr); |
| 345 | + if (err) |
| 346 | + goto out; |
| 347 | + |
| 348 | + err = mnt_want_write_file(file); |
| 349 | + if (err) |
| 350 | + goto out; |
| 351 | + inode_lock(inode); |
| 352 | + |
| 353 | + oldattr = exfat_make_attr(inode); |
| 354 | + |
| 355 | + /* |
| 356 | + * Mask attributes so we don't set reserved fields. |
| 357 | + */ |
| 358 | + attr &= (EXFAT_ATTR_READONLY | EXFAT_ATTR_HIDDEN | EXFAT_ATTR_SYSTEM | |
| 359 | + EXFAT_ATTR_ARCHIVE); |
| 360 | + attr |= (is_dir ? EXFAT_ATTR_SUBDIR : 0); |
| 361 | + |
| 362 | + /* Equivalent to a chmod() */ |
| 363 | + ia.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 364 | + ia.ia_ctime = current_time(inode); |
| 365 | + if (is_dir) |
| 366 | + ia.ia_mode = exfat_make_mode(sbi, attr, 0777); |
| 367 | + else |
| 368 | + ia.ia_mode = exfat_make_mode(sbi, attr, 0666 | (inode->i_mode & 0111)); |
| 369 | + |
| 370 | + /* The root directory has no attributes */ |
| 371 | + if (inode->i_ino == EXFAT_ROOT_INO && attr != EXFAT_ATTR_SUBDIR) { |
| 372 | + err = -EINVAL; |
| 373 | + goto out_unlock_inode; |
| 374 | + } |
| 375 | + |
| 376 | + if (((attr | oldattr) & EXFAT_ATTR_SYSTEM) && |
| 377 | + !capable(CAP_LINUX_IMMUTABLE)) { |
| 378 | + err = -EPERM; |
| 379 | + goto out_unlock_inode; |
| 380 | + } |
| 381 | + |
| 382 | + /* |
| 383 | + * The security check is questionable... We single |
| 384 | + * out the RO attribute for checking by the security |
| 385 | + * module, just because it maps to a file mode. |
| 386 | + */ |
| 387 | + err = security_inode_setattr(file_mnt_idmap(file), |
| 388 | + file->f_path.dentry, &ia); |
| 389 | + if (err) |
| 390 | + goto out_unlock_inode; |
| 391 | + |
| 392 | + /* This MUST be done before doing anything irreversible... */ |
| 393 | + err = exfat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia); |
| 394 | + if (err) |
| 395 | + goto out_unlock_inode; |
| 396 | + |
| 397 | + fsnotify_change(file->f_path.dentry, ia.ia_valid); |
| 398 | + |
| 399 | + exfat_save_attr(inode, attr); |
| 400 | + mark_inode_dirty(inode); |
| 401 | +out_unlock_inode: |
| 402 | + inode_unlock(inode); |
| 403 | + mnt_drop_write_file(file); |
| 404 | +out: |
| 405 | + return err; |
| 406 | +} |
| 407 | + |
318 | 408 | static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg) |
319 | 409 | { |
320 | 410 | struct fstrim_range range; |
@@ -345,8 +435,13 @@ static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg) |
345 | 435 | long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
346 | 436 | { |
347 | 437 | struct inode *inode = file_inode(filp); |
| 438 | + u32 __user *user_attr = (u32 __user *)arg; |
348 | 439 |
|
349 | 440 | switch (cmd) { |
| 441 | + case FAT_IOCTL_GET_ATTRIBUTES: |
| 442 | + return exfat_ioctl_get_attributes(inode, user_attr); |
| 443 | + case FAT_IOCTL_SET_ATTRIBUTES: |
| 444 | + return exfat_ioctl_set_attributes(filp, user_attr); |
350 | 445 | case FITRIM: |
351 | 446 | return exfat_ioctl_fitrim(inode, arg); |
352 | 447 | default: |
|
0 commit comments