Merge branch 'linus' into x86/i8259
[sfrench/cifs-2.6.git] / fs / fat / file.c
index 2a3bed96704148c1377537677c4c7d9edd9372a3..771326b8047e0ff5e3483f885c1edc30a604471a 100644 (file)
@@ -157,104 +157,6 @@ out:
        return err;
 }
 
-static int check_mode(const struct msdos_sb_info *sbi, mode_t mode)
-{
-       mode_t req = mode & ~S_IFMT;
-
-       /*
-        * 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 (S_ISREG(mode)) {
-               req &= ~sbi->options.fs_fmask;
-
-               if ((req & (S_IRUGO | S_IXUGO)) !=
-                   ((S_IRUGO | S_IXUGO) & ~sbi->options.fs_fmask))
-                       return -EPERM;
-
-               if ((req & S_IWUGO) != 0 &&
-                   (req & S_IWUGO) != (S_IWUGO & ~sbi->options.fs_fmask))
-                       return -EPERM;
-       } else if (S_ISDIR(mode)) {
-               req &= ~sbi->options.fs_dmask;
-
-               if ((req & (S_IRUGO | S_IXUGO)) !=
-                   ((S_IRUGO | S_IXUGO) & ~sbi->options.fs_dmask))
-                       return -EPERM;
-
-               if ((req & S_IWUGO) != 0 &&
-                   (req & S_IWUGO) != (S_IWUGO & ~sbi->options.fs_dmask))
-                       return -EPERM;
-       } else {
-               return -EPERM;
-       }
-
-       return 0;
-}
-
-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)))
-               error = -EPERM;
-
-       if (error) {
-               if (sbi->options.quiet)
-                       error = 0;
-               goto out;
-       }
-
-       if (attr->ia_valid & ATTR_MODE) {
-               error = check_mode(sbi, attr->ia_mode);
-               if (error != 0 && !sbi->options.quiet)
-                       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)
 {
@@ -306,7 +208,7 @@ static int fat_free(struct inode *inode, int skip)
                } else if (ret == FAT_ENT_FREE) {
                        fat_fs_panic(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);
@@ -355,8 +257,122 @@ int fat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
 }
 EXPORT_SYMBOL_GPL(fat_getattr);
 
+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 & ~MSDOS_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 ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
+               return -EPERM;
+       if ((perm & S_IWUGO) && ((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;
+}
+
+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;
+       int error = 0;
+       unsigned int ia_valid;
+
+       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;
+               }
+       }
+
+       /* Check for setting the inode time. */
+       ia_valid = attr->ia_valid;
+       if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
+               if (fat_allow_set_time(sbi, inode))
+                       attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
+       }
+
+       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 & ~MSDOS_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;
+       }
+
+       error = inode_setattr(inode, attr);
+out:
+       unlock_kernel();
+       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,
 };