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> |
| 11 | #include <linux/time.h> |
| 12 | #include <linux/msdos_fs.h> |
| 13 | #include <linux/smp_lock.h> |
| 14 | #include <linux/buffer_head.h> |
OGAWA Hirofumi | 05eb0b5 | 2006-01-08 01:02:13 -0800 | [diff] [blame] | 15 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | int fat_generic_ioctl(struct inode *inode, struct file *filp, |
| 18 | unsigned int cmd, unsigned long arg) |
| 19 | { |
| 20 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
| 21 | u32 __user *user_attr = (u32 __user *)arg; |
| 22 | |
| 23 | switch (cmd) { |
| 24 | case FAT_IOCTL_GET_ATTRIBUTES: |
| 25 | { |
| 26 | u32 attr; |
| 27 | |
| 28 | if (inode->i_ino == MSDOS_ROOT_INO) |
| 29 | attr = ATTR_DIR; |
| 30 | else |
| 31 | attr = fat_attr(inode); |
| 32 | |
| 33 | return put_user(attr, user_attr); |
| 34 | } |
| 35 | case FAT_IOCTL_SET_ATTRIBUTES: |
| 36 | { |
| 37 | u32 attr, oldattr; |
| 38 | int err, is_dir = S_ISDIR(inode->i_mode); |
| 39 | struct iattr ia; |
| 40 | |
| 41 | err = get_user(attr, user_attr); |
| 42 | if (err) |
| 43 | return err; |
| 44 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 45 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | if (IS_RDONLY(inode)) { |
| 48 | err = -EROFS; |
| 49 | goto up; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * ATTR_VOLUME and ATTR_DIR cannot be changed; this also |
| 54 | * prevents the user from turning us into a VFAT |
| 55 | * longname entry. Also, we obviously can't set |
| 56 | * any of the NTFS attributes in the high 24 bits. |
| 57 | */ |
| 58 | attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR); |
| 59 | /* Merge in ATTR_VOLUME and ATTR_DIR */ |
| 60 | attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) | |
| 61 | (is_dir ? ATTR_DIR : 0); |
| 62 | oldattr = fat_attr(inode); |
| 63 | |
| 64 | /* Equivalent to a chmod() */ |
| 65 | ia.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 66 | if (is_dir) { |
| 67 | ia.ia_mode = MSDOS_MKMODE(attr, |
| 68 | S_IRWXUGO & ~sbi->options.fs_dmask) |
| 69 | | S_IFDIR; |
| 70 | } else { |
| 71 | ia.ia_mode = MSDOS_MKMODE(attr, |
| 72 | (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO)) |
| 73 | & ~sbi->options.fs_fmask) |
| 74 | | S_IFREG; |
| 75 | } |
| 76 | |
| 77 | /* The root directory has no attributes */ |
| 78 | if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { |
| 79 | err = -EINVAL; |
| 80 | goto up; |
| 81 | } |
| 82 | |
| 83 | if (sbi->options.sys_immutable) { |
| 84 | if ((attr | oldattr) & ATTR_SYS) { |
| 85 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 86 | err = -EPERM; |
| 87 | goto up; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* This MUST be done before doing anything irreversible... */ |
| 93 | err = notify_change(filp->f_dentry, &ia); |
| 94 | if (err) |
| 95 | goto up; |
| 96 | |
| 97 | if (sbi->options.sys_immutable) { |
| 98 | if (attr & ATTR_SYS) |
| 99 | inode->i_flags |= S_IMMUTABLE; |
| 100 | else |
| 101 | inode->i_flags &= S_IMMUTABLE; |
| 102 | } |
| 103 | |
| 104 | MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED; |
| 105 | mark_inode_dirty(inode); |
| 106 | up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 107 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return err; |
| 109 | } |
| 110 | default: |
| 111 | return -ENOTTY; /* Inappropriate ioctl for device */ |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | struct file_operations fat_file_operations = { |
| 116 | .llseek = generic_file_llseek, |
| 117 | .read = do_sync_read, |
| 118 | .write = do_sync_write, |
| 119 | .readv = generic_file_readv, |
OGAWA Hirofumi | ef40226 | 2005-09-16 19:28:13 -0700 | [diff] [blame] | 120 | .writev = generic_file_writev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .aio_read = generic_file_aio_read, |
OGAWA Hirofumi | ef40226 | 2005-09-16 19:28:13 -0700 | [diff] [blame] | 122 | .aio_write = generic_file_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | .mmap = generic_file_mmap, |
| 124 | .ioctl = fat_generic_ioctl, |
| 125 | .fsync = file_fsync, |
| 126 | .sendfile = generic_file_sendfile, |
| 127 | }; |
| 128 | |
OGAWA Hirofumi | 05eb0b5 | 2006-01-08 01:02:13 -0800 | [diff] [blame] | 129 | static int fat_cont_expand(struct inode *inode, loff_t size) |
| 130 | { |
| 131 | struct address_space *mapping = inode->i_mapping; |
| 132 | loff_t start = inode->i_size, count = size - inode->i_size; |
| 133 | int err; |
| 134 | |
| 135 | err = generic_cont_expand_simple(inode, size); |
| 136 | if (err) |
| 137 | goto out; |
| 138 | |
| 139 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 140 | mark_inode_dirty(inode); |
| 141 | if (IS_SYNC(inode)) |
| 142 | err = sync_page_range_nolock(inode, mapping, start, count); |
| 143 | out: |
| 144 | return err; |
| 145 | } |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | int fat_notify_change(struct dentry *dentry, struct iattr *attr) |
| 148 | { |
| 149 | struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); |
| 150 | struct inode *inode = dentry->d_inode; |
| 151 | int mask, error = 0; |
| 152 | |
| 153 | lock_kernel(); |
| 154 | |
OGAWA Hirofumi | 05eb0b5 | 2006-01-08 01:02:13 -0800 | [diff] [blame] | 155 | /* |
| 156 | * Expand the file. Since inode_setattr() updates ->i_size |
| 157 | * before calling the ->truncate(), but FAT needs to fill the |
| 158 | * hole before it. |
| 159 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | if (attr->ia_valid & ATTR_SIZE) { |
| 161 | if (attr->ia_size > inode->i_size) { |
OGAWA Hirofumi | 05eb0b5 | 2006-01-08 01:02:13 -0800 | [diff] [blame] | 162 | error = fat_cont_expand(inode, attr->ia_size); |
| 163 | if (error || attr->ia_valid == ATTR_SIZE) |
| 164 | goto out; |
| 165 | attr->ia_valid &= ~ATTR_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | error = inode_change_ok(inode, attr); |
| 170 | if (error) { |
| 171 | if (sbi->options.quiet) |
| 172 | error = 0; |
| 173 | goto out; |
| 174 | } |
| 175 | if (((attr->ia_valid & ATTR_UID) && |
| 176 | (attr->ia_uid != sbi->options.fs_uid)) || |
| 177 | ((attr->ia_valid & ATTR_GID) && |
| 178 | (attr->ia_gid != sbi->options.fs_gid)) || |
| 179 | ((attr->ia_valid & ATTR_MODE) && |
| 180 | (attr->ia_mode & ~MSDOS_VALID_MODE))) |
| 181 | error = -EPERM; |
| 182 | |
| 183 | if (error) { |
| 184 | if (sbi->options.quiet) |
| 185 | error = 0; |
| 186 | goto out; |
| 187 | } |
| 188 | error = inode_setattr(inode, attr); |
| 189 | if (error) |
| 190 | goto out; |
| 191 | |
| 192 | if (S_ISDIR(inode->i_mode)) |
| 193 | mask = sbi->options.fs_dmask; |
| 194 | else |
| 195 | mask = sbi->options.fs_fmask; |
| 196 | inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask); |
| 197 | out: |
| 198 | unlock_kernel(); |
| 199 | return error; |
| 200 | } |
| 201 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 202 | EXPORT_SYMBOL_GPL(fat_notify_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | /* Free all clusters after the skip'th cluster. */ |
| 205 | static int fat_free(struct inode *inode, int skip) |
| 206 | { |
| 207 | struct super_block *sb = inode->i_sb; |
| 208 | int err, wait, free_start, i_start, i_logstart; |
| 209 | |
| 210 | if (MSDOS_I(inode)->i_start == 0) |
| 211 | return 0; |
| 212 | |
| 213 | /* |
| 214 | * Write a new EOF, and get the remaining cluster chain for freeing. |
| 215 | */ |
| 216 | wait = IS_DIRSYNC(inode); |
| 217 | if (skip) { |
| 218 | struct fat_entry fatent; |
| 219 | int ret, fclus, dclus; |
| 220 | |
| 221 | ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); |
| 222 | if (ret < 0) |
| 223 | return ret; |
| 224 | else if (ret == FAT_ENT_EOF) |
| 225 | return 0; |
| 226 | |
| 227 | fatent_init(&fatent); |
| 228 | ret = fat_ent_read(inode, &fatent, dclus); |
| 229 | if (ret == FAT_ENT_EOF) { |
| 230 | fatent_brelse(&fatent); |
| 231 | return 0; |
| 232 | } else if (ret == FAT_ENT_FREE) { |
| 233 | fat_fs_panic(sb, |
| 234 | "%s: invalid cluster chain (i_pos %lld)", |
| 235 | __FUNCTION__, MSDOS_I(inode)->i_pos); |
| 236 | ret = -EIO; |
| 237 | } else if (ret > 0) { |
| 238 | err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); |
| 239 | if (err) |
| 240 | ret = err; |
| 241 | } |
| 242 | fatent_brelse(&fatent); |
| 243 | if (ret < 0) |
| 244 | return ret; |
| 245 | |
| 246 | free_start = ret; |
| 247 | i_start = i_logstart = 0; |
| 248 | fat_cache_inval_inode(inode); |
| 249 | } else { |
| 250 | fat_cache_inval_inode(inode); |
| 251 | |
| 252 | i_start = free_start = MSDOS_I(inode)->i_start; |
| 253 | i_logstart = MSDOS_I(inode)->i_logstart; |
| 254 | MSDOS_I(inode)->i_start = 0; |
| 255 | MSDOS_I(inode)->i_logstart = 0; |
| 256 | } |
| 257 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; |
| 258 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 259 | if (wait) { |
| 260 | err = fat_sync_inode(inode); |
| 261 | if (err) |
| 262 | goto error; |
| 263 | } else |
| 264 | mark_inode_dirty(inode); |
| 265 | inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); |
| 266 | |
| 267 | /* Freeing the remained cluster chain */ |
| 268 | return fat_free_clusters(inode, free_start); |
| 269 | |
| 270 | error: |
| 271 | if (i_start) { |
| 272 | MSDOS_I(inode)->i_start = i_start; |
| 273 | MSDOS_I(inode)->i_logstart = i_logstart; |
| 274 | } |
| 275 | return err; |
| 276 | } |
| 277 | |
| 278 | void fat_truncate(struct inode *inode) |
| 279 | { |
| 280 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); |
| 281 | const unsigned int cluster_size = sbi->cluster_size; |
| 282 | int nr_clusters; |
| 283 | |
| 284 | /* |
| 285 | * This protects against truncating a file bigger than it was then |
| 286 | * trying to write into the hole. |
| 287 | */ |
| 288 | if (MSDOS_I(inode)->mmu_private > inode->i_size) |
| 289 | MSDOS_I(inode)->mmu_private = inode->i_size; |
| 290 | |
| 291 | nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits; |
| 292 | |
| 293 | lock_kernel(); |
| 294 | fat_free(inode, nr_clusters); |
| 295 | unlock_kernel(); |
| 296 | } |
| 297 | |
| 298 | struct inode_operations fat_file_inode_operations = { |
| 299 | .truncate = fat_truncate, |
| 300 | .setattr = fat_notify_change, |
| 301 | }; |