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