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