Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Inode operations for Coda filesystem |
| 3 | * Original version: (C) 1996 P. Braam and M. Callahan |
| 4 | * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University |
| 5 | * |
| 6 | * Carnegie Mellon encourages users to contribute improvements to |
| 7 | * the Coda project. Contact Peter Braam (coda@cs.cmu.edu). |
| 8 | */ |
| 9 | |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/time.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/stat.h> |
| 15 | #include <linux/errno.h> |
Fabian Frederick | 834b46c | 2014-08-08 14:20:33 -0700 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/string.h> |
| 18 | |
| 19 | #include <linux/coda.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/coda_psdev.h> |
Al Viro | 31a203d | 2011-01-12 16:36:09 -0500 | [diff] [blame] | 21 | #include "coda_linux.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | /* initialize the debugging variables */ |
| 24 | int coda_fake_statfs; |
| 25 | |
| 26 | /* print a fid */ |
| 27 | char * coda_f2s(struct CodaFid *f) |
| 28 | { |
| 29 | static char s[60]; |
Adrian Bunk | de0ca06 | 2008-07-25 01:46:34 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | sprintf(s, "(%08x.%08x.%08x.%08x)", f->opaque[0], f->opaque[1], f->opaque[2], f->opaque[3]); |
Adrian Bunk | de0ca06 | 2008-07-25 01:46:34 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | return s; |
| 34 | } |
| 35 | |
| 36 | /* recognize special .CONTROL name */ |
| 37 | int coda_iscontrol(const char *name, size_t length) |
| 38 | { |
| 39 | return ((CODA_CONTROLLEN == length) && |
| 40 | (strncmp(name, CODA_CONTROL, CODA_CONTROLLEN) == 0)); |
| 41 | } |
| 42 | |
| 43 | /* recognize /coda inode */ |
| 44 | int coda_isroot(struct inode *i) |
| 45 | { |
| 46 | return ( i->i_sb->s_root->d_inode == i ); |
| 47 | } |
| 48 | |
| 49 | unsigned short coda_flags_to_cflags(unsigned short flags) |
| 50 | { |
| 51 | unsigned short coda_flags = 0; |
| 52 | |
| 53 | if ((flags & O_ACCMODE) == O_RDONLY) |
| 54 | coda_flags |= C_O_READ; |
| 55 | |
| 56 | if ((flags & O_ACCMODE) == O_RDWR) |
| 57 | coda_flags |= C_O_READ | C_O_WRITE; |
| 58 | |
| 59 | if ((flags & O_ACCMODE) == O_WRONLY) |
| 60 | coda_flags |= C_O_WRITE; |
| 61 | |
| 62 | if (flags & O_TRUNC) |
| 63 | coda_flags |= C_O_TRUNC; |
| 64 | |
| 65 | if (flags & O_CREAT) |
| 66 | coda_flags |= C_O_CREAT; |
| 67 | |
| 68 | if (flags & O_EXCL) |
| 69 | coda_flags |= C_O_EXCL; |
| 70 | |
| 71 | return coda_flags; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* utility functions below */ |
| 76 | void coda_vattr_to_iattr(struct inode *inode, struct coda_vattr *attr) |
| 77 | { |
| 78 | int inode_type; |
| 79 | /* inode's i_flags, i_ino are set by iget |
| 80 | XXX: is this all we need ?? |
| 81 | */ |
| 82 | switch (attr->va_type) { |
| 83 | case C_VNON: |
| 84 | inode_type = 0; |
| 85 | break; |
| 86 | case C_VREG: |
| 87 | inode_type = S_IFREG; |
| 88 | break; |
| 89 | case C_VDIR: |
| 90 | inode_type = S_IFDIR; |
| 91 | break; |
| 92 | case C_VLNK: |
| 93 | inode_type = S_IFLNK; |
| 94 | break; |
| 95 | default: |
| 96 | inode_type = 0; |
| 97 | } |
| 98 | inode->i_mode |= inode_type; |
| 99 | |
| 100 | if (attr->va_mode != (u_short) -1) |
| 101 | inode->i_mode = attr->va_mode | inode_type; |
| 102 | if (attr->va_uid != -1) |
Eric W. Biederman | d83f590 | 2013-01-30 19:21:14 -0800 | [diff] [blame] | 103 | inode->i_uid = make_kuid(&init_user_ns, (uid_t) attr->va_uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (attr->va_gid != -1) |
Eric W. Biederman | d83f590 | 2013-01-30 19:21:14 -0800 | [diff] [blame] | 105 | inode->i_gid = make_kgid(&init_user_ns, (gid_t) attr->va_gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (attr->va_nlink != -1) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 107 | set_nlink(inode, attr->va_nlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if (attr->va_size != -1) |
| 109 | inode->i_size = attr->va_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (attr->va_size != -1) |
| 111 | inode->i_blocks = (attr->va_size + 511) >> 9; |
| 112 | if (attr->va_atime.tv_sec != -1) |
| 113 | inode->i_atime = attr->va_atime; |
| 114 | if (attr->va_mtime.tv_sec != -1) |
| 115 | inode->i_mtime = attr->va_mtime; |
| 116 | if (attr->va_ctime.tv_sec != -1) |
| 117 | inode->i_ctime = attr->va_ctime; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /* |
| 122 | * BSD sets attributes that need not be modified to -1. |
| 123 | * Linux uses the valid field to indicate what should be |
| 124 | * looked at. The BSD type field needs to be deduced from linux |
| 125 | * mode. |
| 126 | * So we have to do some translations here. |
| 127 | */ |
| 128 | |
| 129 | void coda_iattr_to_vattr(struct iattr *iattr, struct coda_vattr *vattr) |
| 130 | { |
| 131 | unsigned int valid; |
| 132 | |
| 133 | /* clean out */ |
Andrew Morton | a2d416d | 2008-04-29 00:59:22 -0700 | [diff] [blame] | 134 | vattr->va_mode = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | vattr->va_uid = (vuid_t) -1; |
| 136 | vattr->va_gid = (vgid_t) -1; |
| 137 | vattr->va_size = (off_t) -1; |
| 138 | vattr->va_atime.tv_sec = (time_t) -1; |
| 139 | vattr->va_atime.tv_nsec = (time_t) -1; |
| 140 | vattr->va_mtime.tv_sec = (time_t) -1; |
| 141 | vattr->va_mtime.tv_nsec = (time_t) -1; |
| 142 | vattr->va_ctime.tv_sec = (time_t) -1; |
| 143 | vattr->va_ctime.tv_nsec = (time_t) -1; |
| 144 | vattr->va_type = C_VNON; |
| 145 | vattr->va_fileid = -1; |
| 146 | vattr->va_gen = -1; |
| 147 | vattr->va_bytes = -1; |
| 148 | vattr->va_nlink = -1; |
| 149 | vattr->va_blocksize = -1; |
| 150 | vattr->va_rdev = -1; |
| 151 | vattr->va_flags = 0; |
| 152 | |
| 153 | /* determine the type */ |
| 154 | #if 0 |
| 155 | mode = iattr->ia_mode; |
| 156 | if ( S_ISDIR(mode) ) { |
| 157 | vattr->va_type = C_VDIR; |
| 158 | } else if ( S_ISREG(mode) ) { |
| 159 | vattr->va_type = C_VREG; |
| 160 | } else if ( S_ISLNK(mode) ) { |
| 161 | vattr->va_type = C_VLNK; |
| 162 | } else { |
| 163 | /* don't do others */ |
| 164 | vattr->va_type = C_VNON; |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | /* set those vattrs that need change */ |
| 169 | valid = iattr->ia_valid; |
| 170 | if ( valid & ATTR_MODE ) { |
| 171 | vattr->va_mode = iattr->ia_mode; |
| 172 | } |
| 173 | if ( valid & ATTR_UID ) { |
Eric W. Biederman | d83f590 | 2013-01-30 19:21:14 -0800 | [diff] [blame] | 174 | vattr->va_uid = (vuid_t) from_kuid(&init_user_ns, iattr->ia_uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | if ( valid & ATTR_GID ) { |
Eric W. Biederman | d83f590 | 2013-01-30 19:21:14 -0800 | [diff] [blame] | 177 | vattr->va_gid = (vgid_t) from_kgid(&init_user_ns, iattr->ia_gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | if ( valid & ATTR_SIZE ) { |
| 180 | vattr->va_size = iattr->ia_size; |
| 181 | } |
| 182 | if ( valid & ATTR_ATIME ) { |
| 183 | vattr->va_atime = iattr->ia_atime; |
| 184 | } |
| 185 | if ( valid & ATTR_MTIME ) { |
| 186 | vattr->va_mtime = iattr->ia_mtime; |
| 187 | } |
| 188 | if ( valid & ATTR_CTIME ) { |
| 189 | vattr->va_ctime = iattr->ia_ctime; |
| 190 | } |
| 191 | } |
| 192 | |