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/version.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/fs.h>
15 #include <linux/stat.h>
16 #include <linux/errno.h>
17 #include <linux/locks.h>
18 #include <asm/uaccess.h>
19 #include <linux/string.h>
20 
21 #include <linux/coda.h>
22 #include <linux/coda_linux.h>
23 #include <linux/coda_psdev.h>
24 #include <linux/coda_fs_i.h>
25 
26 /* initialize the debugging variables */
27 int coda_debug;
28 int coda_access_cache = 1;
29 int coda_fake_statfs;
30 
31 /* print a fid */
coda_f2s(ViceFid * f)32 char * coda_f2s(ViceFid *f)
33 {
34 	static char s[60];
35 	sprintf(s, "(%-#lx.%-#lx.%-#lx)", f->Volume, f->Vnode, f->Unique);
36 	return s;
37 }
38 
39 /* recognize special .CONTROL name */
coda_iscontrol(const char * name,size_t length)40 int coda_iscontrol(const char *name, size_t length)
41 {
42 	return ((CODA_CONTROLLEN == length) &&
43                 (strncmp(name, CODA_CONTROL, CODA_CONTROLLEN) == 0));
44 }
45 
46 /* recognize /coda inode */
coda_isroot(struct inode * i)47 int coda_isroot(struct inode *i)
48 {
49     return ( i->i_sb->s_root->d_inode == i );
50 }
51 
52 /* put the current process credentials in the cred */
coda_load_creds(struct coda_cred * cred)53 void coda_load_creds(struct coda_cred *cred)
54 {
55         cred->cr_uid = (vuid_t) current->uid;
56         cred->cr_euid = (vuid_t) current->euid;
57         cred->cr_suid = (vuid_t) current->suid;
58         cred->cr_fsuid = (vuid_t) current->fsuid;
59 
60         cred->cr_groupid = (vgid_t) current->gid;
61         cred->cr_egid = (vgid_t) current->egid;
62         cred->cr_sgid = (vgid_t) current->sgid;
63         cred->cr_fsgid = (vgid_t) current->fsgid;
64 }
65 
coda_cred_ok(struct coda_cred * cred)66 int coda_cred_ok(struct coda_cred *cred)
67 {
68 	return(current->fsuid == cred->cr_fsuid);
69 }
70 
coda_cred_eq(struct coda_cred * cred1,struct coda_cred * cred2)71 int coda_cred_eq(struct coda_cred *cred1, struct coda_cred *cred2)
72 {
73 	return (cred1->cr_fsuid == cred2->cr_fsuid);
74 }
75 
coda_flags_to_cflags(unsigned short flags)76 unsigned short coda_flags_to_cflags(unsigned short flags)
77 {
78 	unsigned short coda_flags = 0;
79 
80 	if ( (flags & O_ACCMODE) == O_RDONLY ){
81 		CDEBUG(D_FILE, "--> C_O_READ added\n");
82 		coda_flags |= C_O_READ;
83 	}
84 
85 	if ( (flags & O_ACCMODE) ==  O_RDWR ) {
86 		CDEBUG(D_FILE, "--> C_O_READ | C_O_WRITE added\n");
87 		coda_flags |= C_O_READ | C_O_WRITE;
88 	}
89 
90 	if ( (flags & O_ACCMODE) == O_WRONLY ){
91 		CDEBUG(D_FILE, "--> C_O_WRITE added\n");
92 		coda_flags |= C_O_WRITE;
93 	}
94 
95 	if ( flags & O_TRUNC )  {
96 		CDEBUG(D_FILE, "--> C_O_TRUNC added\n");
97 		coda_flags |= C_O_TRUNC;
98 	}
99 
100 	if ( flags & O_CREAT )  {
101 		CDEBUG(D_FILE, "--> C_O_CREAT added\n");
102 		coda_flags |= C_O_CREAT;
103 	}
104 
105 	if ( flags & O_EXCL ) {
106 		coda_flags |= C_O_EXCL;
107 		CDEBUG(D_FILE, "--> C_O_EXCL added\n");
108 	}
109 
110 	return coda_flags;
111 }
112 
113 
114 /* utility functions below */
coda_vattr_to_iattr(struct inode * inode,struct coda_vattr * attr)115 void coda_vattr_to_iattr(struct inode *inode, struct coda_vattr *attr)
116 {
117         int inode_type;
118         /* inode's i_dev, i_flags, i_ino are set by iget
119            XXX: is this all we need ??
120            */
121         switch (attr->va_type) {
122         case C_VNON:
123                 inode_type  = 0;
124                 break;
125         case C_VREG:
126                 inode_type = S_IFREG;
127                 break;
128         case C_VDIR:
129                 inode_type = S_IFDIR;
130                 break;
131         case C_VLNK:
132                 inode_type = S_IFLNK;
133                 break;
134         default:
135                 inode_type = 0;
136         }
137 	inode->i_mode |= inode_type;
138 
139 	if (attr->va_mode != (u_short) -1)
140 	        inode->i_mode = attr->va_mode | inode_type;
141         if (attr->va_uid != -1)
142 	        inode->i_uid = (uid_t) attr->va_uid;
143         if (attr->va_gid != -1)
144 	        inode->i_gid = (gid_t) attr->va_gid;
145 	if (attr->va_nlink != -1)
146 	        inode->i_nlink = attr->va_nlink;
147 	if (attr->va_size != -1)
148 	        inode->i_size = attr->va_size;
149 	if (attr->va_blocksize != -1)
150 		inode->i_blksize = attr->va_blocksize;
151 	if (attr->va_size != -1)
152 		inode->i_blocks = (attr->va_size + 511) >> 9;
153 	if (attr->va_atime.tv_sec != -1)
154 	        inode->i_atime = attr->va_atime.tv_sec;
155 	if (attr->va_mtime.tv_sec != -1)
156 	        inode->i_mtime = attr->va_mtime.tv_sec;
157         if (attr->va_ctime.tv_sec != -1)
158 	        inode->i_ctime = attr->va_ctime.tv_sec;
159 }
160 
161 
162 /*
163  * BSD sets attributes that need not be modified to -1.
164  * Linux uses the valid field to indicate what should be
165  * looked at.  The BSD type field needs to be deduced from linux
166  * mode.
167  * So we have to do some translations here.
168  */
169 
coda_iattr_to_vattr(struct iattr * iattr,struct coda_vattr * vattr)170 void coda_iattr_to_vattr(struct iattr *iattr, struct coda_vattr *vattr)
171 {
172         unsigned int valid;
173 
174         /* clean out */
175         vattr->va_mode = (umode_t) -1;
176         vattr->va_uid = (vuid_t) -1;
177         vattr->va_gid = (vgid_t) -1;
178         vattr->va_size = (off_t) -1;
179 	vattr->va_atime.tv_sec = (time_t) -1;
180         vattr->va_mtime.tv_sec  = (time_t) -1;
181 	vattr->va_ctime.tv_sec  = (time_t) -1;
182 	vattr->va_atime.tv_nsec =  (time_t) -1;
183         vattr->va_mtime.tv_nsec = (time_t) -1;
184 	vattr->va_ctime.tv_nsec = (time_t) -1;
185         vattr->va_type = C_VNON;
186 	vattr->va_fileid = -1;
187 	vattr->va_gen = -1;
188 	vattr->va_bytes = -1;
189 	vattr->va_nlink = -1;
190 	vattr->va_blocksize = -1;
191 	vattr->va_rdev = -1;
192         vattr->va_flags = 0;
193 
194         /* determine the type */
195 #if 0
196         mode = iattr->ia_mode;
197                 if ( S_ISDIR(mode) ) {
198                 vattr->va_type = C_VDIR;
199         } else if ( S_ISREG(mode) ) {
200                 vattr->va_type = C_VREG;
201         } else if ( S_ISLNK(mode) ) {
202                 vattr->va_type = C_VLNK;
203         } else {
204                 /* don't do others */
205                 vattr->va_type = C_VNON;
206         }
207 #endif
208 
209         /* set those vattrs that need change */
210         valid = iattr->ia_valid;
211         if ( valid & ATTR_MODE ) {
212                 vattr->va_mode = iattr->ia_mode;
213 	}
214         if ( valid & ATTR_UID ) {
215                 vattr->va_uid = (vuid_t) iattr->ia_uid;
216 	}
217         if ( valid & ATTR_GID ) {
218                 vattr->va_gid = (vgid_t) iattr->ia_gid;
219 	}
220         if ( valid & ATTR_SIZE ) {
221                 vattr->va_size = iattr->ia_size;
222 	}
223         if ( valid & ATTR_ATIME ) {
224                 vattr->va_atime.tv_sec = iattr->ia_atime;
225                 vattr->va_atime.tv_nsec = 0;
226 	}
227         if ( valid & ATTR_MTIME ) {
228                 vattr->va_mtime.tv_sec = iattr->ia_mtime;
229                 vattr->va_mtime.tv_nsec = 0;
230 	}
231         if ( valid & ATTR_CTIME ) {
232                 vattr->va_ctime.tv_sec = iattr->ia_ctime;
233                 vattr->va_ctime.tv_nsec = 0;
234 	}
235 }
236 
print_vattr(struct coda_vattr * attr)237 void print_vattr(struct coda_vattr *attr)
238 {
239     char *typestr;
240 
241     switch (attr->va_type) {
242     case C_VNON:
243 	typestr = "C_VNON";
244 	break;
245     case C_VREG:
246 	typestr = "C_VREG";
247 	break;
248     case C_VDIR:
249 	typestr = "C_VDIR";
250 	break;
251     case C_VBLK:
252 	typestr = "C_VBLK";
253 	break;
254     case C_VCHR:
255 	typestr = "C_VCHR";
256 	break;
257     case C_VLNK:
258 	typestr = "C_VLNK";
259 	break;
260     case C_VSOCK:
261 	typestr = "C_VSCK";
262 	break;
263     case C_VFIFO:
264 	typestr = "C_VFFO";
265 	break;
266     case C_VBAD:
267 	typestr = "C_VBAD";
268 	break;
269     default:
270 	typestr = "????";
271 	break;
272     }
273 
274 
275     printk("attr: type %s (%o)  mode %o uid %d gid %d rdev %d\n",
276 	   typestr, (int)attr->va_type, (int)attr->va_mode,
277 	   (int)attr->va_uid, (int)attr->va_gid, (int)attr->va_rdev);
278 
279     printk("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
280 	      (int)attr->va_fileid, (int)attr->va_nlink,
281 	      (int)attr->va_size,
282 	      (int)attr->va_blocksize,(int)attr->va_bytes);
283     printk("      gen %ld flags %ld\n",
284 	      attr->va_gen, attr->va_flags);
285     printk("      atime sec %d nsec %d\n",
286 	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec);
287     printk("      mtime sec %d nsec %d\n",
288 	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec);
289     printk("      ctime sec %d nsec %d\n",
290 	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec);
291 }
292