Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/fat/file.c |
| 3 | * |
| 4 | * Written 1992,1993 by Werner Almesberger |
| 5 | * |
| 6 | * regular file handling primitives for fat-based filesystems |
| 7 | */ |
| 8 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 9 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 11 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/buffer_head.h> |
OGAWA Hirofumi | 05eb0b5 | 2006-01-08 01:02:13 -0800 | [diff] [blame] | 14 | #include <linux/writeback.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 15 | #include <linux/backing-dev.h> |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 16 | #include <linux/blkdev.h> |
Miklos Szeredi | b1da47e | 2008-07-01 15:01:28 +0200 | [diff] [blame] | 17 | #include <linux/fsnotify.h> |
| 18 | #include <linux/security.h> |
OGAWA Hirofumi | 9e975da | 2008-11-06 12:53:46 -0800 | [diff] [blame] | 19 | #include "fat.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | int fat_generic_ioctl(struct inode *inode, struct file *filp, |
| 22 | unsigned int cmd, unsigned long arg) |
| 23 | { |
| 24 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
| 25 | u32 __user *user_attr = (u32 __user *)arg; |
| 26 | |
| 27 | switch (cmd) { |
| 28 | case FAT_IOCTL_GET_ATTRIBUTES: |
| 29 | { |
OGAWA Hirofumi | 9c0aa1b | 2008-11-06 12:53:54 -0800 | [diff] [blame^] | 30 | u32 attr = fat_make_attrs(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | return put_user(attr, user_attr); |
| 32 | } |
| 33 | case FAT_IOCTL_SET_ATTRIBUTES: |
| 34 | { |
| 35 | u32 attr, oldattr; |
| 36 | int err, is_dir = S_ISDIR(inode->i_mode); |
| 37 | struct iattr ia; |
| 38 | |
| 39 | err = get_user(attr, user_attr); |
| 40 | if (err) |
| 41 | return err; |
| 42 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 43 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 45 | err = mnt_want_write(filp->f_path.mnt); |
| 46 | if (err) |
| 47 | goto up_no_drop_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * ATTR_VOLUME and ATTR_DIR cannot be changed; this also |
| 51 | * prevents the user from turning us into a VFAT |
| 52 | * longname entry. Also, we obviously can't set |
| 53 | * any of the NTFS attributes in the high 24 bits. |
| 54 | */ |
| 55 | attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR); |
| 56 | /* Merge in ATTR_VOLUME and ATTR_DIR */ |
| 57 | attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) | |
| 58 | (is_dir ? ATTR_DIR : 0); |
OGAWA Hirofumi | 9c0aa1b | 2008-11-06 12:53:54 -0800 | [diff] [blame^] | 59 | oldattr = fat_make_attrs(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* Equivalent to a chmod() */ |
| 62 | ia.ia_valid = ATTR_MODE | ATTR_CTIME; |
Miklos Szeredi | b1da47e | 2008-07-01 15:01:28 +0200 | [diff] [blame] | 63 | ia.ia_ctime = current_fs_time(inode->i_sb); |
OGAWA Hirofumi | 9c0aa1b | 2008-11-06 12:53:54 -0800 | [diff] [blame^] | 64 | if (is_dir) |
| 65 | ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO); |
| 66 | else { |
| 67 | ia.ia_mode = fat_make_mode(sbi, attr, |
| 68 | S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* The root directory has no attributes */ |
| 72 | if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { |
| 73 | err = -EINVAL; |
| 74 | goto up; |
| 75 | } |
| 76 | |
| 77 | if (sbi->options.sys_immutable) { |
| 78 | if ((attr | oldattr) & ATTR_SYS) { |
| 79 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 80 | err = -EPERM; |
| 81 | goto up; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Miklos Szeredi | b1da47e | 2008-07-01 15:01:28 +0200 | [diff] [blame] | 86 | /* |
| 87 | * The security check is questionable... We single |
| 88 | * out the RO attribute for checking by the security |
| 89 | * module, just because it maps to a file mode. |
| 90 | */ |
| 91 | err = security_inode_setattr(filp->f_path.dentry, &ia); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (err) |
| 93 | goto up; |
| 94 | |
Miklos Szeredi | b1da47e | 2008-07-01 15:01:28 +0200 | [diff] [blame] | 95 | /* This MUST be done before doing anything irreversible... */ |
| 96 | err = fat_setattr(filp->f_path.dentry, &ia); |
| 97 | if (err) |
| 98 | goto up; |
| 99 | |
| 100 | fsnotify_change(filp->f_path.dentry, ia.ia_valid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (sbi->options.sys_immutable) { |
| 102 | if (attr & ATTR_SYS) |
| 103 | inode->i_flags |= S_IMMUTABLE; |
| 104 | else |
| 105 | inode->i_flags &= S_IMMUTABLE; |
| 106 | } |
| 107 | |
OGAWA Hirofumi | 9c0aa1b | 2008-11-06 12:53:54 -0800 | [diff] [blame^] | 108 | fat_save_attrs(inode, attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | mark_inode_dirty(inode); |
Dave Hansen | 42a74f2 | 2008-02-15 14:37:46 -0800 | [diff] [blame] | 110 | up: |
| 111 | mnt_drop_write(filp->f_path.mnt); |
| 112 | up_no_drop_write: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 113 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return err; |
| 115 | } |
| 116 | default: |
| 117 | return -ENOTTY; /* Inappropriate ioctl for device */ |
| 118 | } |
| 119 | } |
| 120 | |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 121 | static int fat_file_release(struct inode *inode, struct file *filp) |
| 122 | { |
| 123 | if ((filp->f_mode & FMODE_WRITE) && |
| 124 | MSDOS_SB(inode->i_sb)->options.flush) { |
| 125 | fat_flush_inodes(inode->i_sb, inode, NULL); |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 126 | congestion_wait(WRITE, HZ/10); |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 127 | } |
| 128 | return 0; |
| 129 | } |
| 130 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 131 | const struct file_operations fat_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .llseek = generic_file_llseek, |
| 133 | .read = do_sync_read, |
| 134 | .write = do_sync_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .aio_read = generic_file_aio_read, |
OGAWA Hirofumi | ef40226 | 2005-09-16 19:28:13 -0700 | [diff] [blame] | 136 | .aio_write = generic_file_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | .mmap = generic_file_mmap, |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 138 | .release = fat_file_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .ioctl = fat_generic_ioctl, |
| 140 | .fsync = file_fsync, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 141 | .splice_read = generic_file_splice_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
OGAWA Hirofumi | 05eb0b5 | 2006-01-08 01:02:13 -0800 | [diff] [blame] | 144 | static int fat_cont_expand(struct inode *inode, loff_t size) |
| 145 | { |
| 146 | struct address_space *mapping = inode->i_mapping; |
| 147 | loff_t start = inode->i_size, count = size - inode->i_size; |
| 148 | int err; |
| 149 | |
| 150 | err = generic_cont_expand_simple(inode, size); |
| 151 | if (err) |
| 152 | goto out; |
| 153 | |
| 154 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 155 | mark_inode_dirty(inode); |
| 156 | if (IS_SYNC(inode)) |
| 157 | err = sync_page_range_nolock(inode, mapping, start, count); |
| 158 | out: |
| 159 | return err; |
| 160 | } |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* Free all clusters after the skip'th cluster. */ |
| 163 | static int fat_free(struct inode *inode, int skip) |
| 164 | { |
| 165 | struct super_block *sb = inode->i_sb; |
| 166 | int err, wait, free_start, i_start, i_logstart; |
| 167 | |
| 168 | if (MSDOS_I(inode)->i_start == 0) |
| 169 | return 0; |
| 170 | |
OGAWA Hirofumi | 3b64140 | 2006-02-03 03:04:44 -0800 | [diff] [blame] | 171 | fat_cache_inval_inode(inode); |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | wait = IS_DIRSYNC(inode); |
OGAWA Hirofumi | 3b64140 | 2006-02-03 03:04:44 -0800 | [diff] [blame] | 174 | i_start = free_start = MSDOS_I(inode)->i_start; |
| 175 | i_logstart = MSDOS_I(inode)->i_logstart; |
| 176 | |
| 177 | /* First, we write the new file size. */ |
| 178 | if (!skip) { |
| 179 | MSDOS_I(inode)->i_start = 0; |
| 180 | MSDOS_I(inode)->i_logstart = 0; |
| 181 | } |
| 182 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; |
| 183 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 184 | if (wait) { |
| 185 | err = fat_sync_inode(inode); |
| 186 | if (err) { |
| 187 | MSDOS_I(inode)->i_start = i_start; |
| 188 | MSDOS_I(inode)->i_logstart = i_logstart; |
| 189 | return err; |
| 190 | } |
| 191 | } else |
| 192 | mark_inode_dirty(inode); |
| 193 | |
| 194 | /* Write a new EOF, and get the remaining cluster chain for freeing. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (skip) { |
| 196 | struct fat_entry fatent; |
| 197 | int ret, fclus, dclus; |
| 198 | |
| 199 | ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); |
| 200 | if (ret < 0) |
| 201 | return ret; |
| 202 | else if (ret == FAT_ENT_EOF) |
| 203 | return 0; |
| 204 | |
| 205 | fatent_init(&fatent); |
| 206 | ret = fat_ent_read(inode, &fatent, dclus); |
| 207 | if (ret == FAT_ENT_EOF) { |
| 208 | fatent_brelse(&fatent); |
| 209 | return 0; |
| 210 | } else if (ret == FAT_ENT_FREE) { |
| 211 | fat_fs_panic(sb, |
| 212 | "%s: invalid cluster chain (i_pos %lld)", |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 213 | __func__, MSDOS_I(inode)->i_pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | ret = -EIO; |
| 215 | } else if (ret > 0) { |
| 216 | err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); |
| 217 | if (err) |
| 218 | ret = err; |
| 219 | } |
| 220 | fatent_brelse(&fatent); |
| 221 | if (ret < 0) |
| 222 | return ret; |
| 223 | |
| 224 | free_start = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); |
| 227 | |
| 228 | /* Freeing the remained cluster chain */ |
| 229 | return fat_free_clusters(inode, free_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void fat_truncate(struct inode *inode) |
| 233 | { |
Jonathan Corbet | 9c20616 | 2008-05-29 17:14:05 -0600 | [diff] [blame] | 234 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | const unsigned int cluster_size = sbi->cluster_size; |
| 236 | int nr_clusters; |
| 237 | |
| 238 | /* |
| 239 | * This protects against truncating a file bigger than it was then |
| 240 | * trying to write into the hole. |
| 241 | */ |
| 242 | if (MSDOS_I(inode)->mmu_private > inode->i_size) |
| 243 | MSDOS_I(inode)->mmu_private = inode->i_size; |
| 244 | |
| 245 | nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits; |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | fat_free(inode, nr_clusters); |
Chris Mason | ae78bf9 | 2006-09-29 02:00:03 -0700 | [diff] [blame] | 248 | fat_flush_inodes(inode->i_sb, inode, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
OGAWA Hirofumi | da63fc7 | 2006-11-16 01:19:28 -0800 | [diff] [blame] | 251 | int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 252 | { |
| 253 | struct inode *inode = dentry->d_inode; |
| 254 | generic_fillattr(inode, stat); |
| 255 | stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size; |
| 256 | return 0; |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(fat_getattr); |
| 259 | |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 260 | static int fat_sanitize_mode(const struct msdos_sb_info *sbi, |
| 261 | struct inode *inode, umode_t *mode_ptr) |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 262 | { |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 263 | mode_t mask, perm; |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 264 | |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 265 | /* |
| 266 | * Note, the basic check is already done by a caller of |
OGAWA Hirofumi | 9c0aa1b | 2008-11-06 12:53:54 -0800 | [diff] [blame^] | 267 | * (attr->ia_mode & ~FAT_VALID_MODE) |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 268 | */ |
| 269 | |
| 270 | if (S_ISREG(inode->i_mode)) |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 271 | mask = sbi->options.fs_fmask; |
| 272 | else |
| 273 | mask = sbi->options.fs_dmask; |
| 274 | |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 275 | perm = *mode_ptr & ~(S_IFMT | mask); |
| 276 | |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 277 | /* |
| 278 | * Of the r and x bits, all (subject to umask) must be present. Of the |
| 279 | * w bits, either all (subject to umask) or none must be present. |
| 280 | */ |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 281 | if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO))) |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 282 | return -EPERM; |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 283 | if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask))) |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 284 | return -EPERM; |
| 285 | |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 286 | *mode_ptr &= S_IFMT | perm; |
| 287 | |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
OGAWA Hirofumi | 1ae43f8 | 2008-04-28 02:16:26 -0700 | [diff] [blame] | 291 | static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode) |
| 292 | { |
| 293 | mode_t allow_utime = sbi->options.allow_utime; |
| 294 | |
| 295 | if (current->fsuid != inode->i_uid) { |
| 296 | if (in_group_p(inode->i_gid)) |
| 297 | allow_utime >>= 3; |
| 298 | if (allow_utime & MAY_WRITE) |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | /* use a default check */ |
| 303 | return 0; |
| 304 | } |
| 305 | |
OGAWA Hirofumi | 1726384 | 2008-08-02 13:59:37 +0900 | [diff] [blame] | 306 | #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET) |
OGAWA Hirofumi | 9c0aa1b | 2008-11-06 12:53:54 -0800 | [diff] [blame^] | 307 | /* valid file mode bits */ |
| 308 | #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO) |
OGAWA Hirofumi | 1726384 | 2008-08-02 13:59:37 +0900 | [diff] [blame] | 309 | |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 310 | int fat_setattr(struct dentry *dentry, struct iattr *attr) |
| 311 | { |
| 312 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); |
| 313 | struct inode *inode = dentry->d_inode; |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 314 | int error = 0; |
OGAWA Hirofumi | 1ae43f8 | 2008-04-28 02:16:26 -0700 | [diff] [blame] | 315 | unsigned int ia_valid; |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 316 | |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 317 | /* |
| 318 | * Expand the file. Since inode_setattr() updates ->i_size |
| 319 | * before calling the ->truncate(), but FAT needs to fill the |
| 320 | * hole before it. |
| 321 | */ |
| 322 | if (attr->ia_valid & ATTR_SIZE) { |
| 323 | if (attr->ia_size > inode->i_size) { |
| 324 | error = fat_cont_expand(inode, attr->ia_size); |
| 325 | if (error || attr->ia_valid == ATTR_SIZE) |
| 326 | goto out; |
| 327 | attr->ia_valid &= ~ATTR_SIZE; |
| 328 | } |
| 329 | } |
| 330 | |
OGAWA Hirofumi | 1ae43f8 | 2008-04-28 02:16:26 -0700 | [diff] [blame] | 331 | /* Check for setting the inode time. */ |
| 332 | ia_valid = attr->ia_valid; |
OGAWA Hirofumi | 1726384 | 2008-08-02 13:59:37 +0900 | [diff] [blame] | 333 | if (ia_valid & TIMES_SET_FLAGS) { |
OGAWA Hirofumi | 1ae43f8 | 2008-04-28 02:16:26 -0700 | [diff] [blame] | 334 | if (fat_allow_set_time(sbi, inode)) |
OGAWA Hirofumi | 1726384 | 2008-08-02 13:59:37 +0900 | [diff] [blame] | 335 | attr->ia_valid &= ~TIMES_SET_FLAGS; |
OGAWA Hirofumi | 1ae43f8 | 2008-04-28 02:16:26 -0700 | [diff] [blame] | 336 | } |
| 337 | |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 338 | error = inode_change_ok(inode, attr); |
OGAWA Hirofumi | 1ae43f8 | 2008-04-28 02:16:26 -0700 | [diff] [blame] | 339 | attr->ia_valid = ia_valid; |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 340 | if (error) { |
| 341 | if (sbi->options.quiet) |
| 342 | error = 0; |
| 343 | goto out; |
| 344 | } |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 345 | |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 346 | if (((attr->ia_valid & ATTR_UID) && |
| 347 | (attr->ia_uid != sbi->options.fs_uid)) || |
| 348 | ((attr->ia_valid & ATTR_GID) && |
OGAWA Hirofumi | e97e8de | 2008-04-28 02:16:26 -0700 | [diff] [blame] | 349 | (attr->ia_gid != sbi->options.fs_gid)) || |
| 350 | ((attr->ia_valid & ATTR_MODE) && |
OGAWA Hirofumi | 9c0aa1b | 2008-11-06 12:53:54 -0800 | [diff] [blame^] | 351 | (attr->ia_mode & ~FAT_VALID_MODE))) |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 352 | error = -EPERM; |
| 353 | |
| 354 | if (error) { |
| 355 | if (sbi->options.quiet) |
| 356 | error = 0; |
| 357 | goto out; |
| 358 | } |
| 359 | |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 360 | /* |
| 361 | * We don't return -EPERM here. Yes, strange, but this is too |
| 362 | * old behavior. |
| 363 | */ |
| 364 | if (attr->ia_valid & ATTR_MODE) { |
| 365 | if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) |
| 366 | attr->ia_valid &= ~ATTR_MODE; |
| 367 | } |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 368 | |
OGAWA Hirofumi | 2d518f8 | 2008-06-12 15:21:28 -0700 | [diff] [blame] | 369 | error = inode_setattr(inode, attr); |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 370 | out: |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 371 | return error; |
| 372 | } |
| 373 | EXPORT_SYMBOL_GPL(fat_setattr); |
| 374 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 375 | const struct inode_operations fat_file_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | .truncate = fat_truncate, |
OGAWA Hirofumi | 1278fdd | 2008-04-28 02:16:25 -0700 | [diff] [blame] | 377 | .setattr = fat_setattr, |
OGAWA Hirofumi | da63fc7 | 2006-11-16 01:19:28 -0800 | [diff] [blame] | 378 | .getattr = fat_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | }; |