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