FAT: add 'errors' mount option
[safe/jmp/linux-2.6] / fs / fat / file.c
index c1237b7..6214287 100644 (file)
@@ -8,13 +8,15 @@
 
 #include <linux/capability.h>
 #include <linux/module.h>
+#include <linux/mount.h>
 #include <linux/time.h>
-#include <linux/msdos_fs.h>
-#include <linux/smp_lock.h>
 #include <linux/buffer_head.h>
 #include <linux/writeback.h>
 #include <linux/backing-dev.h>
 #include <linux/blkdev.h>
+#include <linux/fsnotify.h>
+#include <linux/security.h>
+#include "fat.h"
 
 int fat_generic_ioctl(struct inode *inode, struct file *filp,
                      unsigned int cmd, unsigned long arg)
@@ -27,10 +29,9 @@ int fat_generic_ioctl(struct inode *inode, struct file *filp,
        {
                u32 attr;
 
-               if (inode->i_ino == MSDOS_ROOT_INO)
-                       attr = ATTR_DIR;
-               else
-                       attr = fat_attr(inode);
+               mutex_lock(&inode->i_mutex);
+               attr = fat_make_attrs(inode);
+               mutex_unlock(&inode->i_mutex);
 
                return put_user(attr, user_attr);
        }
@@ -46,10 +47,9 @@ int fat_generic_ioctl(struct inode *inode, struct file *filp,
 
                mutex_lock(&inode->i_mutex);
 
-               if (IS_RDONLY(inode)) {
-                       err = -EROFS;
-                       goto up;
-               }
+               err = mnt_want_write(filp->f_path.mnt);
+               if (err)
+                       goto up_no_drop_write;
 
                /*
                 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
@@ -61,19 +61,16 @@ int fat_generic_ioctl(struct inode *inode, struct file *filp,
                /* Merge in ATTR_VOLUME and ATTR_DIR */
                attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
                        (is_dir ? ATTR_DIR : 0);
-               oldattr = fat_attr(inode);
+               oldattr = fat_make_attrs(inode);
 
                /* Equivalent to a chmod() */
                ia.ia_valid = ATTR_MODE | ATTR_CTIME;
-               if (is_dir) {
-                       ia.ia_mode = MSDOS_MKMODE(attr,
-                               S_IRWXUGO & ~sbi->options.fs_dmask)
-                               | S_IFDIR;
-               } else {
-                       ia.ia_mode = MSDOS_MKMODE(attr,
-                               (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO))
-                               & ~sbi->options.fs_fmask)
-                               | S_IFREG;
+               ia.ia_ctime = current_fs_time(inode->i_sb);
+               if (is_dir)
+                       ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
+               else {
+                       ia.ia_mode = fat_make_mode(sbi, attr,
+                               S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
                }
 
                /* The root directory has no attributes */
@@ -91,11 +88,21 @@ int fat_generic_ioctl(struct inode *inode, struct file *filp,
                        }
                }
 
+               /*
+                * The security check is questionable...  We single
+                * out the RO attribute for checking by the security
+                * module, just because it maps to a file mode.
+                */
+               err = security_inode_setattr(filp->f_path.dentry, &ia);
+               if (err)
+                       goto up;
+
                /* This MUST be done before doing anything irreversible... */
-               err = notify_change(filp->f_path.dentry, &ia);
+               err = fat_setattr(filp->f_path.dentry, &ia);
                if (err)
                        goto up;
 
+               fsnotify_change(filp->f_path.dentry, ia.ia_valid);
                if (sbi->options.sys_immutable) {
                        if (attr & ATTR_SYS)
                                inode->i_flags |= S_IMMUTABLE;
@@ -103,9 +110,11 @@ int fat_generic_ioctl(struct inode *inode, struct file *filp,
                                inode->i_flags &= S_IMMUTABLE;
                }
 
-               MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED;
+               fat_save_attrs(inode, attr);
                mark_inode_dirty(inode);
-       up:
+up:
+               mnt_drop_write(filp->f_path.mnt);
+up_no_drop_write:
                mutex_unlock(&inode->i_mutex);
                return err;
        }
@@ -134,7 +143,7 @@ const struct file_operations fat_file_operations = {
        .release        = fat_file_release,
        .ioctl          = fat_generic_ioctl,
        .fsync          = file_fsync,
-       .sendfile       = generic_file_sendfile,
+       .splice_read    = generic_file_splice_read,
 };
 
 static int fat_cont_expand(struct inode *inode, loff_t size)
@@ -155,63 +164,6 @@ out:
        return err;
 }
 
-int fat_notify_change(struct dentry *dentry, struct iattr *attr)
-{
-       struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
-       struct inode *inode = dentry->d_inode;
-       int mask, error = 0;
-
-       lock_kernel();
-
-       /*
-        * Expand the file. Since inode_setattr() updates ->i_size
-        * before calling the ->truncate(), but FAT needs to fill the
-        * hole before it.
-        */
-       if (attr->ia_valid & ATTR_SIZE) {
-               if (attr->ia_size > inode->i_size) {
-                       error = fat_cont_expand(inode, attr->ia_size);
-                       if (error || attr->ia_valid == ATTR_SIZE)
-                               goto out;
-                       attr->ia_valid &= ~ATTR_SIZE;
-               }
-       }
-
-       error = inode_change_ok(inode, attr);
-       if (error) {
-               if (sbi->options.quiet)
-                       error = 0;
-               goto out;
-       }
-       if (((attr->ia_valid & ATTR_UID) &&
-            (attr->ia_uid != sbi->options.fs_uid)) ||
-           ((attr->ia_valid & ATTR_GID) &&
-            (attr->ia_gid != sbi->options.fs_gid)) ||
-           ((attr->ia_valid & ATTR_MODE) &&
-            (attr->ia_mode & ~MSDOS_VALID_MODE)))
-               error = -EPERM;
-
-       if (error) {
-               if (sbi->options.quiet)
-                       error = 0;
-               goto out;
-       }
-       error = inode_setattr(inode, attr);
-       if (error)
-               goto out;
-
-       if (S_ISDIR(inode->i_mode))
-               mask = sbi->options.fs_dmask;
-       else
-               mask = sbi->options.fs_fmask;
-       inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
-out:
-       unlock_kernel();
-       return error;
-}
-
-EXPORT_SYMBOL_GPL(fat_notify_change);
-
 /* Free all clusters after the skip'th cluster. */
 static int fat_free(struct inode *inode, int skip)
 {
@@ -261,9 +213,9 @@ static int fat_free(struct inode *inode, int skip)
                        fatent_brelse(&fatent);
                        return 0;
                } else if (ret == FAT_ENT_FREE) {
-                       fat_fs_panic(sb,
+                       fat_fs_error(sb,
                                     "%s: invalid cluster chain (i_pos %lld)",
-                                    __FUNCTION__, MSDOS_I(inode)->i_pos);
+                                    __func__, MSDOS_I(inode)->i_pos);
                        ret = -EIO;
                } else if (ret > 0) {
                        err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
@@ -297,9 +249,7 @@ void fat_truncate(struct inode *inode)
 
        nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits;
 
-       lock_kernel();
        fat_free(inode, nr_clusters);
-       unlock_kernel();
        fat_flush_inodes(inode->i_sb, inode, NULL);
 }
 
@@ -312,8 +262,131 @@ int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
 }
 EXPORT_SYMBOL_GPL(fat_getattr);
 
-struct inode_operations fat_file_inode_operations = {
+static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
+                            struct inode *inode, umode_t *mode_ptr)
+{
+       mode_t mask, perm;
+
+       /*
+        * Note, the basic check is already done by a caller of
+        * (attr->ia_mode & ~FAT_VALID_MODE)
+        */
+
+       if (S_ISREG(inode->i_mode))
+               mask = sbi->options.fs_fmask;
+       else
+               mask = sbi->options.fs_dmask;
+
+       perm = *mode_ptr & ~(S_IFMT | mask);
+
+       /*
+        * Of the r and x bits, all (subject to umask) must be present. Of the
+        * w bits, either all (subject to umask) or none must be present.
+        *
+        * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
+        */
+       if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
+               return -EPERM;
+       if (fat_mode_can_hold_ro(inode)) {
+               if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
+                       return -EPERM;
+       } else {
+               if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
+                       return -EPERM;
+       }
+
+       *mode_ptr &= S_IFMT | perm;
+
+       return 0;
+}
+
+static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
+{
+       mode_t allow_utime = sbi->options.allow_utime;
+
+       if (current_fsuid() != inode->i_uid) {
+               if (in_group_p(inode->i_gid))
+                       allow_utime >>= 3;
+               if (allow_utime & MAY_WRITE)
+                       return 1;
+       }
+
+       /* use a default check */
+       return 0;
+}
+
+#define TIMES_SET_FLAGS        (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
+/* valid file mode bits */
+#define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO)
+
+int fat_setattr(struct dentry *dentry, struct iattr *attr)
+{
+       struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
+       struct inode *inode = dentry->d_inode;
+       unsigned int ia_valid;
+       int error;
+
+       /*
+        * Expand the file. Since inode_setattr() updates ->i_size
+        * before calling the ->truncate(), but FAT needs to fill the
+        * hole before it.
+        */
+       if (attr->ia_valid & ATTR_SIZE) {
+               if (attr->ia_size > inode->i_size) {
+                       error = fat_cont_expand(inode, attr->ia_size);
+                       if (error || attr->ia_valid == ATTR_SIZE)
+                               goto out;
+                       attr->ia_valid &= ~ATTR_SIZE;
+               }
+       }
+
+       /* Check for setting the inode time. */
+       ia_valid = attr->ia_valid;
+       if (ia_valid & TIMES_SET_FLAGS) {
+               if (fat_allow_set_time(sbi, inode))
+                       attr->ia_valid &= ~TIMES_SET_FLAGS;
+       }
+
+       error = inode_change_ok(inode, attr);
+       attr->ia_valid = ia_valid;
+       if (error) {
+               if (sbi->options.quiet)
+                       error = 0;
+               goto out;
+       }
+
+       if (((attr->ia_valid & ATTR_UID) &&
+            (attr->ia_uid != sbi->options.fs_uid)) ||
+           ((attr->ia_valid & ATTR_GID) &&
+            (attr->ia_gid != sbi->options.fs_gid)) ||
+           ((attr->ia_valid & ATTR_MODE) &&
+            (attr->ia_mode & ~FAT_VALID_MODE)))
+               error = -EPERM;
+
+       if (error) {
+               if (sbi->options.quiet)
+                       error = 0;
+               goto out;
+       }
+
+       /*
+        * We don't return -EPERM here. Yes, strange, but this is too
+        * old behavior.
+        */
+       if (attr->ia_valid & ATTR_MODE) {
+               if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
+                       attr->ia_valid &= ~ATTR_MODE;
+       }
+
+       if (attr->ia_valid)
+               error = inode_setattr(inode, attr);
+out:
+       return error;
+}
+EXPORT_SYMBOL_GPL(fat_setattr);
+
+const struct inode_operations fat_file_inode_operations = {
        .truncate       = fat_truncate,
-       .setattr        = fat_notify_change,
+       .setattr        = fat_setattr,
        .getattr        = fat_getattr,
 };