4 * Written 1992,1993 by Werner Almesberger
6 * regular file handling primitives for fat-based filesystems
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>
15 static ssize_t
fat_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
16 size_t count
, loff_t pos
)
18 struct inode
*inode
= iocb
->ki_filp
->f_dentry
->d_inode
;
21 retval
= generic_file_aio_write(iocb
, buf
, count
, pos
);
23 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
24 MSDOS_I(inode
)->i_attrs
|= ATTR_ARCH
;
25 mark_inode_dirty(inode
);
26 // check the locking rules
27 // if (IS_SYNC(inode))
28 // fat_sync_inode(inode);
33 static ssize_t
fat_file_writev(struct file
*filp
, const struct iovec
*iov
,
34 unsigned long nr_segs
, loff_t
*ppos
)
36 struct inode
*inode
= filp
->f_dentry
->d_inode
;
39 retval
= generic_file_writev(filp
, iov
, nr_segs
, ppos
);
41 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
42 MSDOS_I(inode
)->i_attrs
|= ATTR_ARCH
;
43 mark_inode_dirty(inode
);
48 int fat_generic_ioctl(struct inode
*inode
, struct file
*filp
,
49 unsigned int cmd
, unsigned long arg
)
51 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
52 u32 __user
*user_attr
= (u32 __user
*)arg
;
55 case FAT_IOCTL_GET_ATTRIBUTES
:
59 if (inode
->i_ino
== MSDOS_ROOT_INO
)
62 attr
= fat_attr(inode
);
64 return put_user(attr
, user_attr
);
66 case FAT_IOCTL_SET_ATTRIBUTES
:
69 int err
, is_dir
= S_ISDIR(inode
->i_mode
);
72 err
= get_user(attr
, user_attr
);
78 if (IS_RDONLY(inode
)) {
84 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
85 * prevents the user from turning us into a VFAT
86 * longname entry. Also, we obviously can't set
87 * any of the NTFS attributes in the high 24 bits.
89 attr
&= 0xff & ~(ATTR_VOLUME
| ATTR_DIR
);
90 /* Merge in ATTR_VOLUME and ATTR_DIR */
91 attr
|= (MSDOS_I(inode
)->i_attrs
& ATTR_VOLUME
) |
92 (is_dir
? ATTR_DIR
: 0);
93 oldattr
= fat_attr(inode
);
95 /* Equivalent to a chmod() */
96 ia
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
98 ia
.ia_mode
= MSDOS_MKMODE(attr
,
99 S_IRWXUGO
& ~sbi
->options
.fs_dmask
)
102 ia
.ia_mode
= MSDOS_MKMODE(attr
,
103 (S_IRUGO
| S_IWUGO
| (inode
->i_mode
& S_IXUGO
))
104 & ~sbi
->options
.fs_fmask
)
108 /* The root directory has no attributes */
109 if (inode
->i_ino
== MSDOS_ROOT_INO
&& attr
!= ATTR_DIR
) {
114 if (sbi
->options
.sys_immutable
) {
115 if ((attr
| oldattr
) & ATTR_SYS
) {
116 if (!capable(CAP_LINUX_IMMUTABLE
)) {
123 /* This MUST be done before doing anything irreversible... */
124 err
= notify_change(filp
->f_dentry
, &ia
);
128 if (sbi
->options
.sys_immutable
) {
130 inode
->i_flags
|= S_IMMUTABLE
;
132 inode
->i_flags
&= S_IMMUTABLE
;
135 MSDOS_I(inode
)->i_attrs
= attr
& ATTR_UNUSED
;
136 mark_inode_dirty(inode
);
142 return -ENOTTY
; /* Inappropriate ioctl for device */
146 struct file_operations fat_file_operations
= {
147 .llseek
= generic_file_llseek
,
148 .read
= do_sync_read
,
149 .write
= do_sync_write
,
150 .readv
= generic_file_readv
,
151 .writev
= fat_file_writev
,
152 .aio_read
= generic_file_aio_read
,
153 .aio_write
= fat_file_aio_write
,
154 .mmap
= generic_file_mmap
,
155 .ioctl
= fat_generic_ioctl
,
157 .sendfile
= generic_file_sendfile
,
160 int fat_notify_change(struct dentry
*dentry
, struct iattr
*attr
)
162 struct msdos_sb_info
*sbi
= MSDOS_SB(dentry
->d_sb
);
163 struct inode
*inode
= dentry
->d_inode
;
168 /* FAT cannot truncate to a longer file */
169 if (attr
->ia_valid
& ATTR_SIZE
) {
170 if (attr
->ia_size
> inode
->i_size
) {
176 error
= inode_change_ok(inode
, attr
);
178 if (sbi
->options
.quiet
)
182 if (((attr
->ia_valid
& ATTR_UID
) &&
183 (attr
->ia_uid
!= sbi
->options
.fs_uid
)) ||
184 ((attr
->ia_valid
& ATTR_GID
) &&
185 (attr
->ia_gid
!= sbi
->options
.fs_gid
)) ||
186 ((attr
->ia_valid
& ATTR_MODE
) &&
187 (attr
->ia_mode
& ~MSDOS_VALID_MODE
)))
191 if (sbi
->options
.quiet
)
195 error
= inode_setattr(inode
, attr
);
199 if (S_ISDIR(inode
->i_mode
))
200 mask
= sbi
->options
.fs_dmask
;
202 mask
= sbi
->options
.fs_fmask
;
203 inode
->i_mode
&= S_IFMT
| (S_IRWXUGO
& ~mask
);
209 EXPORT_SYMBOL(fat_notify_change
);
211 /* Free all clusters after the skip'th cluster. */
212 static int fat_free(struct inode
*inode
, int skip
)
214 struct super_block
*sb
= inode
->i_sb
;
215 int err
, wait
, free_start
, i_start
, i_logstart
;
217 if (MSDOS_I(inode
)->i_start
== 0)
221 * Write a new EOF, and get the remaining cluster chain for freeing.
223 wait
= IS_DIRSYNC(inode
);
225 struct fat_entry fatent
;
226 int ret
, fclus
, dclus
;
228 ret
= fat_get_cluster(inode
, skip
- 1, &fclus
, &dclus
);
231 else if (ret
== FAT_ENT_EOF
)
234 fatent_init(&fatent
);
235 ret
= fat_ent_read(inode
, &fatent
, dclus
);
236 if (ret
== FAT_ENT_EOF
) {
237 fatent_brelse(&fatent
);
239 } else if (ret
== FAT_ENT_FREE
) {
241 "%s: invalid cluster chain (i_pos %lld)",
242 __FUNCTION__
, MSDOS_I(inode
)->i_pos
);
244 } else if (ret
> 0) {
245 err
= fat_ent_write(inode
, &fatent
, FAT_ENT_EOF
, wait
);
249 fatent_brelse(&fatent
);
254 i_start
= i_logstart
= 0;
255 fat_cache_inval_inode(inode
);
257 fat_cache_inval_inode(inode
);
259 i_start
= free_start
= MSDOS_I(inode
)->i_start
;
260 i_logstart
= MSDOS_I(inode
)->i_logstart
;
261 MSDOS_I(inode
)->i_start
= 0;
262 MSDOS_I(inode
)->i_logstart
= 0;
264 MSDOS_I(inode
)->i_attrs
|= ATTR_ARCH
;
265 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
267 err
= fat_sync_inode(inode
);
271 mark_inode_dirty(inode
);
272 inode
->i_blocks
= skip
<< (MSDOS_SB(sb
)->cluster_bits
- 9);
274 /* Freeing the remained cluster chain */
275 return fat_free_clusters(inode
, free_start
);
279 MSDOS_I(inode
)->i_start
= i_start
;
280 MSDOS_I(inode
)->i_logstart
= i_logstart
;
285 void fat_truncate(struct inode
*inode
)
287 struct msdos_sb_info
*sbi
= MSDOS_SB(inode
->i_sb
);
288 const unsigned int cluster_size
= sbi
->cluster_size
;
292 * This protects against truncating a file bigger than it was then
293 * trying to write into the hole.
295 if (MSDOS_I(inode
)->mmu_private
> inode
->i_size
)
296 MSDOS_I(inode
)->mmu_private
= inode
->i_size
;
298 nr_clusters
= (inode
->i_size
+ (cluster_size
- 1)) >> sbi
->cluster_bits
;
301 fat_free(inode
, nr_clusters
);
305 struct inode_operations fat_file_inode_operations
= {
306 .truncate
= fat_truncate
,
307 .setattr
= fat_notify_change
,