| Start/ | End/ | |||
| True | False | - | Line | Source |
| 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/capability.h> | |||
| 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> | |||
| 15 | #include <linux/writeback.h> | |||
| 16 | ||||
| 0 | 0 | - | 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) { | |||
| 0 | - | 24 | case FAT_IOCTL_GET_ATTRIBUTES: | |
| 25 | { | |||
| 26 | u32 attr; | |||
| 27 | ||||
| 0 | 0 | - | 28 | if (inode->i_ino == MSDOS_ROOT_INO) |
| 29 | attr = ATTR_DIR; | |||
| 30 | else | |||
| 31 | attr = fat_attr(inode); | |||
| 32 | ||||
| 0 | - | 33 | return put_user(attr, user_attr); | |
| 34 | } | |||
| 0 | - | 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); | |||
| 0 | 0 | - | 42 | if (err) |
| 0 | - | 43 | return err; | |
| 44 | ||||
| 45 | mutex_lock(&inode->i_mutex); | |||
| 46 | ||||
| 0 | 0 | - | 47 | if (IS_RDONLY(inode)) { |
| 48 | err = -EROFS; | |||
| 0 | - | 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); | |||
| 0 | 0 | - | 61 | ternary-?: is_dir |
| 62 | oldattr = fat_attr(inode); | |||
| 63 | ||||
| 64 | /* Equivalent to a chmod() */ | |||
| 65 | ia.ia_valid = ATTR_MODE | ATTR_CTIME; | |||
| 0 | 0 | - | 66 | if (is_dir) { |
| 67 | ia.ia_mode = MSDOS_MKMODE(attr, | |||
| 0 | 0 | - | 67 | ternary-?: attr & 1 |
| 68 | S_IRWXUGO & ~sbi->options.fs_dmask) | |||
| 69 | | S_IFDIR; | |||
| 70 | } else { | |||
| 71 | ia.ia_mode = MSDOS_MKMODE(attr, | |||
| 0 | 0 | - | 71 | ternary-?: attr & 1 |
| 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 */ | |||
| 0 | 0 | - | 78 | if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { |
| 0 | - | 78 | T && T | |
| 0 | - | 78 | T && F | |
| 0 | - | 78 | F && _ | |
| 79 | err = -EINVAL; | |||
| 0 | - | 80 | goto up; | |
| 81 | } | |||
| 82 | ||||
| 0 | 0 | - | 83 | if (sbi->options.sys_immutable) { |
| 0 | 0 | - | 84 | if ((attr | oldattr) & ATTR_SYS) { |
| 0 | 0 | - | 85 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 86 | err = -EPERM; | |||
| 0 | - | 87 | goto up; | |
| 88 | } | |||
| 89 | } | |||
| 90 | } | |||
| 91 | ||||
| 92 | /* This MUST be done before doing anything irreversible... */ | |||
| 93 | err = notify_change(filp->f_dentry, &ia); | |||
| 0 | 0 | - | 94 | if (err) |
| 0 | - | 95 | goto up; | |
| 96 | ||||
| 0 | 0 | - | 97 | if (sbi->options.sys_immutable) { |
| 0 | 0 | - | 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: | |||
| 107 | mutex_unlock(&inode->i_mutex); | |||
| 0 | - | 108 | return err; | |
| 109 | } | |||
| 0 | - | 110 | default: | |
| 0 | - | 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, | |||
| 120 | .writev = generic_file_writev, | |||
| 121 | .aio_read = generic_file_aio_read, | |||
| 122 | .aio_write = generic_file_aio_write, | |||
| 123 | .mmap = generic_file_mmap, | |||
| 124 | .ioctl = fat_generic_ioctl, | |||
| 125 | .fsync = file_fsync, | |||
| 126 | .sendfile = generic_file_sendfile, | |||
| 127 | }; | |||
| 128 | ||||
| 0 | 0 | - | 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); | |||
| 0 | 0 | - | 136 | if (err) |
| 0 | - | 137 | goto out; | |
| 138 | ||||
| 139 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; | |||
| 140 | mark_inode_dirty(inode); | |||
| 0 | 0 | - | 141 | if (IS_SYNC(inode)) |
| 0 | - | 141 | ((T) || (_)) | |
| 0 | - | 141 | ((F) || (T)) | |
| 0 | - | 141 | ((F) || (F)) | |
| 142 | err = sync_page_range_nolock(inode, mapping, start, count); | |||
| 143 | out: | |||
| 0 | - | 144 | return err; | |
| 145 | } | |||
| 146 | ||||
| 3 | 0 | 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(); | |||
| 0 | 3 | - | 153 | do-while (0) |
| 154 | ||||
| 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 | */ | |||
| 0 | 3 | - | 160 | if (attr->ia_valid & ATTR_SIZE) { |
| 0 | 0 | - | 161 | if (attr->ia_size > inode->i_size) { |
| 162 | error = fat_cont_expand(inode, attr->ia_size); | |||
| 0 | 0 | - | 163 | if (error || attr->ia_valid == ATTR_SIZE) |
| 0 | - | 163 | T || _ | |
| 0 | - | 163 | F || T | |
| 0 | - | 163 | F || F | |
| 0 | - | 164 | goto out; | |
| 165 | attr->ia_valid &= ~ATTR_SIZE; | |||
| 166 | } | |||
| 167 | } | |||
| 168 | ||||
| 169 | error = inode_change_ok(inode, attr); | |||
| 0 | 3 | - | 170 | if (error) { |
| 0 | 0 | - | 171 | if (sbi->options.quiet) |
| 172 | error = 0; | |||
| 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) && | |||
| 1 | 2 | 180 | (attr->ia_mode & ~MSDOS_VALID_MODE))) | |
| 0 | - | 180 | ((T) && (T)) || ((_) && (_)) || ((_) && (_)) | |
| 1 | 180 | ((T) && (F)) || ((T) && (T)) || ((_) && (_)) | ||
| 0 | - | 180 | ((T) && (F)) || ((T) && (F)) || ((T) && (T)) | |
| 0 | - | 180 | ((T) && (F)) || ((F) && (_)) || ((T) && (T)) | |
| 0 | - | 180 | ((F) && (_)) || ((T) && (T)) || ((_) && (_)) | |
| 0 | - | 180 | ((F) && (_)) || ((T) && (F)) || ((T) && (T)) | |
| 0 | - | 180 | ((F) && (_)) || ((F) && (_)) || ((T) && (T)) | |
| 0 | - | 180 | ((T) && (F)) || ((T) && (F)) || ((T) && (F)) | |
| 0 | - | 180 | ((T) && (F)) || ((T) && (F)) || ((F) && (_)) | |
| 0 | - | 180 | ((T) && (F)) || ((F) && (_)) || ((T) && (F)) | |
| 0 | - | 180 | ((T) && (F)) || ((F) && (_)) || ((F) && (_)) | |
| 0 | - | 180 | ((F) && (_)) || ((T) && (F)) || ((T) && (F)) | |
| 0 | - | 180 | ((F) && (_)) || ((T) && (F)) || ((F) && (_)) | |
| 1 | 180 | ((F) && (_)) || ((F) && (_)) || ((T) && (F)) | ||
| 1 | 180 | ((F) && (_)) || ((F) && (_)) || ((F) && (_)) | ||
| 181 | error = -EPERM; | |||
| 182 | ||||
| 1 | 2 | 183 | if (error) { | |
| 0 | 1 | - | 184 | if (sbi->options.quiet) |
| 185 | error = 0; | |||
| 1 | 186 | goto out; | ||
| 187 | } | |||
| 188 | error = inode_setattr(inode, attr); | |||
| 0 | 2 | - | 189 | if (error) |
| 0 | - | 190 | goto out; | |
| 191 | ||||
| 0 | 2 | - | 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(); | |||
| 0 | 3 | - | 198 | do-while (0) |
| 3 | 199 | return error; | ||
| 200 | } | |||
| 201 | ||||
| 202 | EXPORT_SYMBOL_GPL(fat_notify_change); | |||
| 203 | ||||
| 204 | /* Free all clusters after the skip'th cluster. */ | |||
| 0 | 0 | - | 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 | ||||
| 0 | 0 | - | 210 | if (MSDOS_I(inode)->i_start == 0) |
| 0 | - | 211 | return 0; | |
| 212 | ||||
| 213 | fat_cache_inval_inode(inode); | |||
| 214 | ||||
| 215 | wait = IS_DIRSYNC(inode); | |||
| 216 | i_start = free_start = MSDOS_I(inode)->i_start; | |||
| 217 | i_logstart = MSDOS_I(inode)->i_logstart; | |||
| 218 | ||||
| 219 | /* First, we write the new file size. */ | |||
| 0 | 0 | - | 220 | if (!skip) { |
| 221 | MSDOS_I(inode)->i_start = 0; | |||
| 222 | MSDOS_I(inode)->i_logstart = 0; | |||
| 223 | } | |||
| 224 | MSDOS_I(inode)->i_attrs |= ATTR_ARCH; | |||
| 225 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; | |||
| 0 | 0 | - | 226 | if (wait) { |
| 227 | err = fat_sync_inode(inode); | |||
| 0 | 0 | - | 228 | if (err) { |
| 229 | MSDOS_I(inode)->i_start = i_start; | |||
| 230 | MSDOS_I(inode)->i_logstart = i_logstart; | |||
| 0 | - | 231 | return err; | |
| 232 | } | |||
| 233 | } else | |||
| 234 | mark_inode_dirty(inode); | |||
| 235 | ||||
| 236 | /* Write a new EOF, and get the remaining cluster chain for freeing. */ | |||
| 0 | 0 | - | 237 | if (skip) { |
| 238 | struct fat_entry fatent; | |||
| 239 | int ret, fclus, dclus; | |||
| 240 | ||||
| 241 | ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); | |||
| 0 | 0 | - | 242 | if (ret < 0) |
| 0 | - | 243 | return ret; | |
| 0 | 0 | - | 244 | else if (ret == FAT_ENT_EOF) |
| 0 | - | 245 | return 0; | |
| 246 | ||||
| 247 | fatent_init(&fatent); | |||
| 248 | ret = fat_ent_read(inode, &fatent, dclus); | |||
| 0 | 0 | - | 249 | if (ret == FAT_ENT_EOF) { |
| 250 | fatent_brelse(&fatent); | |||
| 0 | - | 251 | return 0; | |
| 0 | 0 | - | 252 | } else if (ret == FAT_ENT_FREE) { |
| 253 | fat_fs_panic(sb, | |||
| 254 | "%s: invalid cluster chain (i_pos %lld)", | |||
| 255 | __FUNCTION__, MSDOS_I(inode)->i_pos); | |||
| 256 | ret = -EIO; | |||
| 0 | 0 | - | 257 | } else if (ret > 0) { |
| 258 | err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); | |||
| 0 | 0 | - | 259 | if (err) |
| 260 | ret = err; | |||
| 261 | } | |||
| 262 | fatent_brelse(&fatent); | |||
| 0 | 0 | - | 263 | if (ret < 0) |
| 0 | - | 264 | return ret; | |
| 265 | ||||
| 266 | free_start = ret; | |||
| 267 | } | |||
| 268 | inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); | |||
| 269 | ||||
| 270 | /* Freeing the remained cluster chain */ | |||
| 0 | - | 271 | return fat_free_clusters(inode, free_start); | |
| 272 | } | |||
| 273 | ||||
| 0 | 0 | - | 274 | void fat_truncate(struct inode *inode) |
| 275 | { | |||
| 276 | struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); | |||
| 277 | const unsigned int cluster_size = sbi->cluster_size; | |||
| 278 | int nr_clusters; | |||
| 279 | ||||
| 280 | /* | |||
| 281 | * This protects against truncating a file bigger than it was then | |||
| 282 | * trying to write into the hole. | |||
| 283 | */ | |||
| 0 | 0 | - | 284 | if (MSDOS_I(inode)->mmu_private > inode->i_size) |
| 285 | MSDOS_I(inode)->mmu_private = inode->i_size; | |||
| 286 | ||||
| 287 | nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits; | |||
| 288 | ||||
| 289 | lock_kernel(); | |||
| 0 | 0 | - | 289 | do-while (0) |
| 290 | fat_free(inode, nr_clusters); | |||
| 291 | unlock_kernel(); | |||
| 0 | 0 | - | 291 | do-while (0) |
| 292 | } | |||
| 293 | ||||
| 294 | struct inode_operations fat_file_inode_operations = { | |||
| 295 | .truncate = fat_truncate, | |||
| 296 | .setattr = fat_notify_change, | |||
| 297 | }; | |||
| ***TER 12% (17/140) of SOURCE FILE file.c | ||||