/*
       * linux/fs/ext2/ioctl.c
       *
       * Copyright (C) 1993, 1994, 1995
       * Remy Card (card@masi.ibp.fr)
       * Laboratoire MASI - Institut Blaise Pascal
       * Universite Pierre et Marie Curie (Paris VI)
       */
      
      #include <linux/fs.h>
      #include <linux/ext2_fs.h>
      #include <linux/sched.h>
  13  #include <asm/uaccess.h>
      
      
      int ext2_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
      		unsigned long arg)
      {
  19  	unsigned int flags;
  20  
      	ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
      
      	switch (cmd) {
      	case EXT2_IOC_GETFLAGS:
  25  		flags = inode->u.ext2_i.i_flags & EXT2_FL_USER_VISIBLE;
  26  		return put_user(flags, (int *) arg);
  27  	case EXT2_IOC_SETFLAGS: {
  28  		unsigned int oldflags;
  29  
  30  		if (IS_RDONLY(inode))
      			return -EROFS;
      
  33  		if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
      			return -EPERM;
  35  
  36  		if (get_user(flags, (int *) arg))
  37  			return -EFAULT;
  38  
  39  		oldflags = inode->u.ext2_i.i_flags;
  40  
      		/*
  42  		 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  43  		 * the relevant capability.
  44  		 *
      		 * This test looks nicer. Thanks to Pauline Middelink
      		 */
      		if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
  48  			if (!capable(CAP_LINUX_IMMUTABLE))
      				return -EPERM;
      		}
      
      		flags = flags & EXT2_FL_USER_MODIFIABLE;
      		flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
      		inode->u.ext2_i.i_flags = flags;
  55  
  56  		if (flags & EXT2_SYNC_FL)
      			inode->i_flags |= S_SYNC;
  58  		else
  59  			inode->i_flags &= ~S_SYNC;
  60  		if (flags & EXT2_APPEND_FL)
      			inode->i_flags |= S_APPEND;
  62  		else
      			inode->i_flags &= ~S_APPEND;
  64  		if (flags & EXT2_IMMUTABLE_FL)
      			inode->i_flags |= S_IMMUTABLE;
  66  		else
      			inode->i_flags &= ~S_IMMUTABLE;
  68  		if (flags & EXT2_NOATIME_FL)
  69  			inode->i_flags |= S_NOATIME;
  70  		else
      			inode->i_flags &= ~S_NOATIME;
      		inode->i_ctime = CURRENT_TIME;
      		mark_inode_dirty(inode);
      		return 0;
      	}
      	case EXT2_IOC_GETVERSION:
  77  		return put_user(inode->i_generation, (int *) arg);
      	case EXT2_IOC_SETVERSION:
  79  		if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
      			return -EPERM;
  81  		if (IS_RDONLY(inode))
      			return -EROFS;
  83  		if (get_user(inode->i_generation, (int *) arg))
  84  			return -EFAULT;	
  85  		inode->i_ctime = CURRENT_TIME;
      		mark_inode_dirty(inode);
      		return 0;
      	default:
  89  		return -ENOTTY;
  90  	}
      }