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