linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / fs / open.c
1 /*
2  *  linux/fs/open.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/string.h>
8 #include <linux/mm.h>
9 #include <linux/utime.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/quotaops.h>
13 #include <linux/fsnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/namei.h>
18 #include <linux/backing-dev.h>
19 #include <linux/capability.h>
20 #include <linux/security.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <linux/fcntl.h>
24 #include <asm/uaccess.h>
25 #include <linux/fs.h>
26 #include <linux/personality.h>
27 #include <linux/pagemap.h>
28 #include <linux/syscalls.h>
29 #include <linux/rcupdate.h>
30 #include <linux/vs_limit.h>
31 #include <linux/vs_dlimit.h>
32 #include <linux/vserver/xid.h>
33
34 #include <asm/unistd.h>
35
36 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
37 {
38         int retval = -ENODEV;
39
40         if (sb) {
41                 retval = -ENOSYS;
42                 if (sb->s_op->statfs) {
43                         memset(buf, 0, sizeof(*buf));
44                         retval = security_sb_statfs(sb);
45                         if (retval)
46                                 return retval;
47                         retval = sb->s_op->statfs(sb, buf);
48                         if (retval == 0 && buf->f_frsize == 0)
49                                 buf->f_frsize = buf->f_bsize;
50                 }
51                 if (!vx_check(0, VX_ADMIN|VX_WATCH))
52                         vx_vsi_statfs(sb, buf);
53         }
54         return retval;
55 }
56
57 EXPORT_SYMBOL(vfs_statfs);
58
59 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
60 {
61         struct kstatfs st;
62         int retval;
63
64         retval = vfs_statfs(sb, &st);
65         if (retval)
66                 return retval;
67
68         if (sizeof(*buf) == sizeof(st))
69                 memcpy(buf, &st, sizeof(st));
70         else {
71                 if (sizeof buf->f_blocks == 4) {
72                         if ((st.f_blocks | st.f_bfree | st.f_bavail) &
73                             0xffffffff00000000ULL)
74                                 return -EOVERFLOW;
75                         /*
76                          * f_files and f_ffree may be -1; it's okay to stuff
77                          * that into 32 bits
78                          */
79                         if (st.f_files != -1 &&
80                             (st.f_files & 0xffffffff00000000ULL))
81                                 return -EOVERFLOW;
82                         if (st.f_ffree != -1 &&
83                             (st.f_ffree & 0xffffffff00000000ULL))
84                                 return -EOVERFLOW;
85                 }
86
87                 buf->f_type = st.f_type;
88                 buf->f_bsize = st.f_bsize;
89                 buf->f_blocks = st.f_blocks;
90                 buf->f_bfree = st.f_bfree;
91                 buf->f_bavail = st.f_bavail;
92                 buf->f_files = st.f_files;
93                 buf->f_ffree = st.f_ffree;
94                 buf->f_fsid = st.f_fsid;
95                 buf->f_namelen = st.f_namelen;
96                 buf->f_frsize = st.f_frsize;
97                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
98         }
99         return 0;
100 }
101
102 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
103 {
104         struct kstatfs st;
105         int retval;
106
107         retval = vfs_statfs(sb, &st);
108         if (retval)
109                 return retval;
110
111         if (sizeof(*buf) == sizeof(st))
112                 memcpy(buf, &st, sizeof(st));
113         else {
114                 buf->f_type = st.f_type;
115                 buf->f_bsize = st.f_bsize;
116                 buf->f_blocks = st.f_blocks;
117                 buf->f_bfree = st.f_bfree;
118                 buf->f_bavail = st.f_bavail;
119                 buf->f_files = st.f_files;
120                 buf->f_ffree = st.f_ffree;
121                 buf->f_fsid = st.f_fsid;
122                 buf->f_namelen = st.f_namelen;
123                 buf->f_frsize = st.f_frsize;
124                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
125         }
126         return 0;
127 }
128
129 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
130 {
131         struct nameidata nd;
132         int error;
133
134         error = user_path_walk(path, &nd);
135         if (!error) {
136                 struct statfs tmp;
137                 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
138                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
139                         error = -EFAULT;
140                 path_release(&nd);
141         }
142         return error;
143 }
144
145
146 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
147 {
148         struct nameidata nd;
149         long error;
150
151         if (sz != sizeof(*buf))
152                 return -EINVAL;
153         error = user_path_walk(path, &nd);
154         if (!error) {
155                 struct statfs64 tmp;
156                 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
157                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
158                         error = -EFAULT;
159                 path_release(&nd);
160         }
161         return error;
162 }
163
164
165 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
166 {
167         struct file * file;
168         struct statfs tmp;
169         int error;
170
171         error = -EBADF;
172         file = fget(fd);
173         if (!file)
174                 goto out;
175         error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
176         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
177                 error = -EFAULT;
178         fput(file);
179 out:
180         return error;
181 }
182
183 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
184 {
185         struct file * file;
186         struct statfs64 tmp;
187         int error;
188
189         if (sz != sizeof(*buf))
190                 return -EINVAL;
191
192         error = -EBADF;
193         file = fget(fd);
194         if (!file)
195                 goto out;
196         error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
197         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
198                 error = -EFAULT;
199         fput(file);
200 out:
201         return error;
202 }
203
204 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
205         struct file *filp)
206 {
207         int err;
208         struct iattr newattrs;
209
210         /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
211         if (length < 0)
212                 return -EINVAL;
213
214         newattrs.ia_size = length;
215         newattrs.ia_valid = ATTR_SIZE | time_attrs;
216         if (filp) {
217                 newattrs.ia_file = filp;
218                 newattrs.ia_valid |= ATTR_FILE;
219         }
220
221         mutex_lock(&dentry->d_inode->i_mutex);
222         err = notify_change(dentry, &newattrs);
223         mutex_unlock(&dentry->d_inode->i_mutex);
224         return err;
225 }
226
227 static long do_sys_truncate(const char __user * path, loff_t length)
228 {
229         struct nameidata nd;
230         struct inode * inode;
231         int error;
232
233         error = -EINVAL;
234         if (length < 0) /* sorry, but loff_t says... */
235                 goto out;
236
237         error = user_path_walk(path, &nd);
238         if (error)
239                 goto out;
240         inode = nd.dentry->d_inode;
241
242         /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
243         error = -EISDIR;
244         if (S_ISDIR(inode->i_mode))
245                 goto dput_and_out;
246
247         error = -EINVAL;
248         if (!S_ISREG(inode->i_mode))
249                 goto dput_and_out;
250
251         error = vfs_permission(&nd, MAY_WRITE);
252         if (error)
253                 goto dput_and_out;
254
255         error = -EROFS;
256         if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
257                 goto dput_and_out;
258
259         error = -EPERM;
260         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
261                 goto dput_and_out;
262
263         /*
264          * Make sure that there are no leases.
265          */
266         error = break_lease(inode, FMODE_WRITE);
267         if (error)
268                 goto dput_and_out;
269
270         error = get_write_access(inode);
271         if (error)
272                 goto dput_and_out;
273
274         error = locks_verify_truncate(inode, NULL, length);
275         if (!error) {
276                 DQUOT_INIT(inode);
277                 error = do_truncate(nd.dentry, length, 0, NULL);
278         }
279         put_write_access(inode);
280
281 dput_and_out:
282         path_release(&nd);
283 out:
284         return error;
285 }
286
287 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
288 {
289         /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
290         return do_sys_truncate(path, (long)length);
291 }
292
293 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
294 {
295         struct inode * inode;
296         struct dentry *dentry;
297         struct file * file;
298         int error;
299
300         error = -EINVAL;
301         if (length < 0)
302                 goto out;
303         error = -EBADF;
304         file = fget(fd);
305         if (!file)
306                 goto out;
307
308         /* explicitly opened as large or we are on 64-bit box */
309         if (file->f_flags & O_LARGEFILE)
310                 small = 0;
311
312         dentry = file->f_dentry;
313         inode = dentry->d_inode;
314         error = -EINVAL;
315         if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
316                 goto out_putf;
317
318         error = -EINVAL;
319         /* Cannot ftruncate over 2^31 bytes without large file support */
320         if (small && length > MAX_NON_LFS)
321                 goto out_putf;
322
323         error = -EPERM;
324         if (IS_APPEND(inode))
325                 goto out_putf;
326
327         error = locks_verify_truncate(inode, file, length);
328         if (!error)
329                 error = do_truncate(dentry, length, 0, file);
330 out_putf:
331         fput(file);
332 out:
333         return error;
334 }
335
336 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
337 {
338         long ret = do_sys_ftruncate(fd, length, 1);
339         /* avoid REGPARM breakage on x86: */
340         prevent_tail_call(ret);
341         return ret;
342 }
343
344 /* LFS versions of truncate are only needed on 32 bit machines */
345 #if BITS_PER_LONG == 32
346 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
347 {
348         return do_sys_truncate(path, length);
349 }
350
351 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
352 {
353         long ret = do_sys_ftruncate(fd, length, 0);
354         /* avoid REGPARM breakage on x86: */
355         prevent_tail_call(ret);
356         return ret;
357 }
358 #endif
359
360 #ifdef __ARCH_WANT_SYS_UTIME
361
362 /*
363  * sys_utime() can be implemented in user-level using sys_utimes().
364  * Is this for backwards compatibility?  If so, why not move it
365  * into the appropriate arch directory (for those architectures that
366  * need it).
367  */
368
369 /* If times==NULL, set access and modification to current time,
370  * must be owner or have write permission.
371  * Else, update from *times, must be owner or super user.
372  */
373 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
374 {
375         int error;
376         struct nameidata nd;
377         struct inode * inode;
378         struct iattr newattrs;
379
380         error = user_path_walk(filename, &nd);
381         if (error)
382                 goto out;
383         inode = nd.dentry->d_inode;
384
385         error = -EROFS;
386         if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
387                 goto dput_and_out;
388
389         /* Don't worry, the checks are done in inode_change_ok() */
390         newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
391         if (times) {
392                 error = -EPERM;
393                 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
394                         goto dput_and_out;
395
396                 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
397                 newattrs.ia_atime.tv_nsec = 0;
398                 if (!error)
399                         error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
400                 newattrs.ia_mtime.tv_nsec = 0;
401                 if (error)
402                         goto dput_and_out;
403
404                 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
405         } else {
406                 error = -EACCES;
407                 if (IS_IMMUTABLE(inode))
408                         goto dput_and_out;
409
410                 if (current->fsuid != inode->i_uid &&
411                     (error = vfs_permission(&nd, MAY_WRITE)) != 0)
412                         goto dput_and_out;
413         }
414         mutex_lock(&inode->i_mutex);
415         error = notify_change(nd.dentry, &newattrs);
416         mutex_unlock(&inode->i_mutex);
417 dput_and_out:
418         path_release(&nd);
419 out:
420         return error;
421 }
422
423 #endif
424
425 /* If times==NULL, set access and modification to current time,
426  * must be owner or have write permission.
427  * Else, update from *times, must be owner or super user.
428  */
429 long do_utimes(int dfd, char __user *filename, struct timeval *times)
430 {
431         int error;
432         struct nameidata nd;
433         struct inode * inode;
434         struct iattr newattrs;
435
436         error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
437
438         if (error)
439                 goto out;
440         inode = nd.dentry->d_inode;
441
442         error = -EROFS;
443         if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
444                 goto dput_and_out;
445
446         /* Don't worry, the checks are done in inode_change_ok() */
447         newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
448         if (times) {
449                 error = -EPERM;
450                 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
451                         goto dput_and_out;
452
453                 newattrs.ia_atime.tv_sec = times[0].tv_sec;
454                 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
455                 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
456                 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
457                 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
458         } else {
459                 error = -EACCES;
460                 if (IS_IMMUTABLE(inode))
461                         goto dput_and_out;
462
463                 if (current->fsuid != inode->i_uid &&
464                     (error = vfs_permission(&nd, MAY_WRITE)) != 0)
465                         goto dput_and_out;
466         }
467         mutex_lock(&inode->i_mutex);
468         error = notify_change(nd.dentry, &newattrs);
469         mutex_unlock(&inode->i_mutex);
470 dput_and_out:
471         path_release(&nd);
472 out:
473         return error;
474 }
475
476 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
477 {
478         struct timeval times[2];
479
480         if (utimes && copy_from_user(&times, utimes, sizeof(times)))
481                 return -EFAULT;
482         return do_utimes(dfd, filename, utimes ? times : NULL);
483 }
484
485 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
486 {
487         return sys_futimesat(AT_FDCWD, filename, utimes);
488 }
489
490
491 /*
492  * access() needs to use the real uid/gid, not the effective uid/gid.
493  * We do this by temporarily clearing all FS-related capabilities and
494  * switching the fsuid/fsgid around to the real ones.
495  */
496 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
497 {
498         struct nameidata nd;
499         int old_fsuid, old_fsgid;
500         kernel_cap_t old_cap;
501         int res;
502
503         if (mode & ~S_IRWXO)    /* where's F_OK, X_OK, W_OK, R_OK? */
504                 return -EINVAL;
505
506         old_fsuid = current->fsuid;
507         old_fsgid = current->fsgid;
508         old_cap = current->cap_effective;
509
510         current->fsuid = current->uid;
511         current->fsgid = current->gid;
512
513         /*
514          * Clear the capabilities if we switch to a non-root user
515          *
516          * FIXME: There is a race here against sys_capset.  The
517          * capabilities can change yet we will restore the old
518          * value below.  We should hold task_capabilities_lock,
519          * but we cannot because user_path_walk can sleep.
520          */
521         if (current->uid)
522                 cap_clear(current->cap_effective);
523         else
524                 current->cap_effective = current->cap_permitted;
525
526         res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
527         if (!res) {
528                 res = vfs_permission(&nd, mode);
529                 /* SuS v2 requires we report a read only fs too */
530                 if(!res && (mode & S_IWOTH)
531                    && (IS_RDONLY(nd.dentry->d_inode) || MNT_IS_RDONLY(nd.mnt))
532                    && !special_file(nd.dentry->d_inode->i_mode))
533                         res = -EROFS;
534                 path_release(&nd);
535         }
536
537         current->fsuid = old_fsuid;
538         current->fsgid = old_fsgid;
539         current->cap_effective = old_cap;
540
541         return res;
542 }
543
544 asmlinkage long sys_access(const char __user *filename, int mode)
545 {
546         return sys_faccessat(AT_FDCWD, filename, mode);
547 }
548
549 asmlinkage long sys_chdir(const char __user * filename)
550 {
551         struct nameidata nd;
552         int error;
553
554         error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
555         if (error)
556                 goto out;
557
558         error = vfs_permission(&nd, MAY_EXEC);
559         if (error)
560                 goto dput_and_out;
561
562         set_fs_pwd(current->fs, nd.mnt, nd.dentry);
563
564 dput_and_out:
565         path_release(&nd);
566 out:
567         return error;
568 }
569
570 asmlinkage long sys_fchdir(unsigned int fd)
571 {
572         struct file *file;
573         struct dentry *dentry;
574         struct inode *inode;
575         struct vfsmount *mnt;
576         int error;
577
578         error = -EBADF;
579         file = fget(fd);
580         if (!file)
581                 goto out;
582
583         dentry = file->f_dentry;
584         mnt = file->f_vfsmnt;
585         inode = dentry->d_inode;
586
587         error = -ENOTDIR;
588         if (!S_ISDIR(inode->i_mode))
589                 goto out_putf;
590
591         error = file_permission(file, MAY_EXEC);
592         if (!error)
593                 set_fs_pwd(current->fs, mnt, dentry);
594 out_putf:
595         fput(file);
596 out:
597         return error;
598 }
599
600 asmlinkage long sys_chroot(const char __user * filename)
601 {
602         struct nameidata nd;
603         int error;
604
605         error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
606         if (error)
607                 goto out;
608
609         error = vfs_permission(&nd, MAY_EXEC);
610         if (error)
611                 goto dput_and_out;
612
613         error = -EPERM;
614         if (!capable(CAP_SYS_CHROOT))
615                 goto dput_and_out;
616
617         set_fs_root(current->fs, nd.mnt, nd.dentry);
618         set_fs_altroot();
619         error = 0;
620 dput_and_out:
621         path_release(&nd);
622 out:
623         return error;
624 }
625
626 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
627 {
628         struct inode * inode;
629         struct dentry * dentry;
630         struct file * file;
631         int err = -EBADF;
632         struct iattr newattrs;
633
634         file = fget(fd);
635         if (!file)
636                 goto out;
637
638         dentry = file->f_dentry;
639         inode = dentry->d_inode;
640
641         err = -EROFS;
642         if (IS_RDONLY(inode) || MNT_IS_RDONLY(file->f_vfsmnt))
643                 goto out_putf;
644         err = -EPERM;
645         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
646                 goto out_putf;
647         mutex_lock(&inode->i_mutex);
648         if (mode == (mode_t) -1)
649                 mode = inode->i_mode;
650         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
651         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
652         err = notify_change(dentry, &newattrs);
653         mutex_unlock(&inode->i_mutex);
654
655 out_putf:
656         fput(file);
657 out:
658         return err;
659 }
660
661 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
662                              mode_t mode)
663 {
664         struct nameidata nd;
665         struct inode * inode;
666         int error;
667         struct iattr newattrs;
668
669         error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
670         if (error)
671                 goto out;
672         inode = nd.dentry->d_inode;
673
674         error = -EROFS;
675         if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
676                 goto dput_and_out;
677
678         error = -EPERM;
679         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
680                 goto dput_and_out;
681
682         mutex_lock(&inode->i_mutex);
683         if (mode == (mode_t) -1)
684                 mode = inode->i_mode;
685         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
686         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
687         error = notify_change(nd.dentry, &newattrs);
688         mutex_unlock(&inode->i_mutex);
689
690 dput_and_out:
691         path_release(&nd);
692 out:
693         return error;
694 }
695
696 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
697 {
698         return sys_fchmodat(AT_FDCWD, filename, mode);
699 }
700
701 static int chown_common(struct dentry *dentry, struct vfsmount *mnt,
702         uid_t user, gid_t group)
703 {
704         struct inode * inode;
705         int error;
706         struct iattr newattrs;
707
708         error = -ENOENT;
709         if (!(inode = dentry->d_inode)) {
710                 printk(KERN_ERR "chown_common: NULL inode\n");
711                 goto out;
712         }
713         error = -EROFS;
714         if (IS_RDONLY(inode) || MNT_IS_RDONLY(mnt))
715                 goto out;
716         error = -EPERM;
717         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
718                 goto out;
719         newattrs.ia_valid =  ATTR_CTIME;
720         if (user != (uid_t) -1) {
721                 newattrs.ia_valid |= ATTR_UID;
722                 newattrs.ia_uid = vx_map_uid(user);
723         }
724         if (group != (gid_t) -1) {
725                 newattrs.ia_valid |= ATTR_GID;
726                 newattrs.ia_gid = vx_map_gid(group);
727         }
728         if (!S_ISDIR(inode->i_mode))
729                 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
730         mutex_lock(&inode->i_mutex);
731         error = notify_change(dentry, &newattrs);
732         mutex_unlock(&inode->i_mutex);
733 out:
734         return error;
735 }
736
737 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
738 {
739         struct nameidata nd;
740         int error;
741
742         error = user_path_walk(filename, &nd);
743         if (!error) {
744                 error = chown_common(nd.dentry, nd.mnt, user, group);
745                 path_release(&nd);
746         }
747         return error;
748 }
749
750 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
751                              gid_t group, int flag)
752 {
753         struct nameidata nd;
754         int error = -EINVAL;
755         int follow;
756
757         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
758                 goto out;
759
760         follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
761         error = __user_walk_fd(dfd, filename, follow, &nd);
762         if (!error) {
763                 error = chown_common(nd.dentry, nd.mnt, user, group);
764                 path_release(&nd);
765         }
766 out:
767         return error;
768 }
769
770 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
771 {
772         struct nameidata nd;
773         int error;
774
775         error = user_path_walk_link(filename, &nd);
776         if (!error) {
777                 error = chown_common(nd.dentry, nd.mnt, user, group);
778                 path_release(&nd);
779         }
780         return error;
781 }
782
783
784 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
785 {
786         struct file * file;
787         int error = -EBADF;
788
789         file = fget(fd);
790         if (file) {
791                 error = chown_common(file->f_dentry, file->f_vfsmnt, user, group);
792                 fput(file);
793         }
794         return error;
795 }
796
797 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
798                                         int flags, struct file *f,
799                                         int (*open)(struct inode *, struct file *))
800 {
801         struct inode *inode;
802         int error;
803
804         f->f_flags = flags;
805         f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
806                                 FMODE_PREAD | FMODE_PWRITE;
807         inode = dentry->d_inode;
808         if (f->f_mode & FMODE_WRITE) {
809                 error = get_write_access(inode);
810                 if (error)
811                         goto cleanup_file;
812         }
813
814         f->f_mapping = inode->i_mapping;
815         f->f_dentry = dentry;
816         f->f_vfsmnt = mnt;
817         f->f_pos = 0;
818         f->f_op = fops_get(inode->i_fop);
819         file_move(f, &inode->i_sb->s_files);
820
821         if (!open && f->f_op)
822                 open = f->f_op->open;
823         if (open) {
824                 error = open(inode, f);
825                 if (error)
826                         goto cleanup_all;
827         }
828
829         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
830
831         file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
832
833         /* NB: we're sure to have correct a_ops only after f_op->open */
834         if (f->f_flags & O_DIRECT) {
835                 if (!f->f_mapping->a_ops ||
836                     ((!f->f_mapping->a_ops->direct_IO) &&
837                     (!f->f_mapping->a_ops->get_xip_page))) {
838                         fput(f);
839                         f = ERR_PTR(-EINVAL);
840                 }
841         }
842
843         return f;
844
845 cleanup_all:
846         fops_put(f->f_op);
847         if (f->f_mode & FMODE_WRITE)
848                 put_write_access(inode);
849         file_kill(f);
850         f->f_dentry = NULL;
851         f->f_vfsmnt = NULL;
852 cleanup_file:
853         put_filp(f);
854         dput(dentry);
855         mntput(mnt);
856         return ERR_PTR(error);
857 }
858
859 /*
860  * Note that while the flag value (low two bits) for sys_open means:
861  *      00 - read-only
862  *      01 - write-only
863  *      10 - read-write
864  *      11 - special
865  * it is changed into
866  *      00 - no permissions needed
867  *      01 - read-permission
868  *      10 - write-permission
869  *      11 - read-write
870  * for the internal routines (ie open_namei()/follow_link() etc). 00 is
871  * used by symlinks.
872  */
873 static struct file *do_filp_open(int dfd, const char *filename, int flags,
874                                  int mode)
875 {
876         int namei_flags, error;
877         struct nameidata nd;
878
879         namei_flags = flags;
880         if ((namei_flags+1) & O_ACCMODE)
881                 namei_flags++;
882
883         error = open_namei(dfd, filename, namei_flags, mode, &nd);
884         if (!error)
885                 return nameidata_to_filp(&nd, flags);
886
887         return ERR_PTR(error);
888 }
889
890 struct file *filp_open(const char *filename, int flags, int mode)
891 {
892         return do_filp_open(AT_FDCWD, filename, flags, mode);
893 }
894 EXPORT_SYMBOL(filp_open);
895
896 /**
897  * lookup_instantiate_filp - instantiates the open intent filp
898  * @nd: pointer to nameidata
899  * @dentry: pointer to dentry
900  * @open: open callback
901  *
902  * Helper for filesystems that want to use lookup open intents and pass back
903  * a fully instantiated struct file to the caller.
904  * This function is meant to be called from within a filesystem's
905  * lookup method.
906  * Note that in case of error, nd->intent.open.file is destroyed, but the
907  * path information remains valid.
908  * If the open callback is set to NULL, then the standard f_op->open()
909  * filesystem callback is substituted.
910  */
911 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
912                 int (*open)(struct inode *, struct file *))
913 {
914         if (IS_ERR(nd->intent.open.file))
915                 goto out;
916         if (IS_ERR(dentry))
917                 goto out_err;
918         nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
919                                              nd->intent.open.flags - 1,
920                                              nd->intent.open.file,
921                                              open);
922 out:
923         return nd->intent.open.file;
924 out_err:
925         release_open_intent(nd);
926         nd->intent.open.file = (struct file *)dentry;
927         goto out;
928 }
929 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
930
931 /**
932  * nameidata_to_filp - convert a nameidata to an open filp.
933  * @nd: pointer to nameidata
934  * @flags: open flags
935  *
936  * Note that this function destroys the original nameidata
937  */
938 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
939 {
940         struct file *filp;
941
942         /* Pick up the filp from the open intent */
943         filp = nd->intent.open.file;
944         /* Has the filesystem initialised the file for us? */
945         if (filp->f_dentry == NULL)
946                 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
947         else
948                 path_release(nd);
949         return filp;
950 }
951
952 /*
953  * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
954  * error.
955  */
956 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
957 {
958         int error;
959         struct file *f;
960
961         error = -ENFILE;
962         f = get_empty_filp();
963         if (f == NULL) {
964                 dput(dentry);
965                 mntput(mnt);
966                 return ERR_PTR(error);
967         }
968
969         return __dentry_open(dentry, mnt, flags, f, NULL);
970 }
971 EXPORT_SYMBOL(dentry_open);
972
973 /*
974  * Find an empty file descriptor entry, and mark it busy.
975  */
976 int get_unused_fd(void)
977 {
978         struct files_struct * files = current->files;
979         int fd, error;
980         struct fdtable *fdt;
981
982         error = -EMFILE;
983         spin_lock(&files->file_lock);
984
985 repeat:
986         fdt = files_fdtable(files);
987         fd = find_next_zero_bit(fdt->open_fds->fds_bits,
988                                 fdt->max_fdset,
989                                 fdt->next_fd);
990
991         /*
992          * N.B. For clone tasks sharing a files structure, this test
993          * will limit the total number of files that can be opened.
994          */
995         if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
996                 goto out;
997
998         /* Do we need to expand the fd array or fd set?  */
999         error = expand_files(files, fd);
1000         if (error < 0)
1001                 goto out;
1002
1003         if (error) {
1004                 /*
1005                  * If we needed to expand the fs array we
1006                  * might have blocked - try again.
1007                  */
1008                 error = -EMFILE;
1009                 goto repeat;
1010         }
1011
1012         FD_SET(fd, fdt->open_fds);
1013         FD_CLR(fd, fdt->close_on_exec);
1014         fdt->next_fd = fd + 1;
1015         vx_openfd_inc(fd);
1016 #if 1
1017         /* Sanity check */
1018         if (fdt->fd[fd] != NULL) {
1019                 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
1020                 fdt->fd[fd] = NULL;
1021         }
1022 #endif
1023         error = fd;
1024
1025 out:
1026         spin_unlock(&files->file_lock);
1027         return error;
1028 }
1029
1030 EXPORT_SYMBOL(get_unused_fd);
1031
1032 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1033 {
1034         struct fdtable *fdt = files_fdtable(files);
1035         __FD_CLR(fd, fdt->open_fds);
1036         if (fd < fdt->next_fd)
1037                 fdt->next_fd = fd;
1038         vx_openfd_dec(fd);
1039 }
1040
1041 void fastcall put_unused_fd(unsigned int fd)
1042 {
1043         struct files_struct *files = current->files;
1044         spin_lock(&files->file_lock);
1045         __put_unused_fd(files, fd);
1046         spin_unlock(&files->file_lock);
1047 }
1048
1049 EXPORT_SYMBOL(put_unused_fd);
1050
1051 /*
1052  * Install a file pointer in the fd array.
1053  *
1054  * The VFS is full of places where we drop the files lock between
1055  * setting the open_fds bitmap and installing the file in the file
1056  * array.  At any such point, we are vulnerable to a dup2() race
1057  * installing a file in the array before us.  We need to detect this and
1058  * fput() the struct file we are about to overwrite in this case.
1059  *
1060  * It should never happen - if we allow dup2() do it, _really_ bad things
1061  * will follow.
1062  */
1063
1064 void fastcall fd_install(unsigned int fd, struct file * file)
1065 {
1066         struct files_struct *files = current->files;
1067         struct fdtable *fdt;
1068         spin_lock(&files->file_lock);
1069         fdt = files_fdtable(files);
1070         BUG_ON(fdt->fd[fd] != NULL);
1071         rcu_assign_pointer(fdt->fd[fd], file);
1072         spin_unlock(&files->file_lock);
1073 }
1074
1075 EXPORT_SYMBOL(fd_install);
1076
1077 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1078 {
1079         char *tmp = getname(filename);
1080         int fd = PTR_ERR(tmp);
1081
1082         if (!IS_ERR(tmp)) {
1083                 fd = get_unused_fd();
1084                 if (fd >= 0) {
1085                         struct file *f = do_filp_open(dfd, tmp, flags, mode);
1086                         if (IS_ERR(f)) {
1087                                 put_unused_fd(fd);
1088                                 fd = PTR_ERR(f);
1089                         } else {
1090                                 fsnotify_open(f->f_dentry);
1091                                 fd_install(fd, f);
1092                         }
1093                 }
1094                 putname(tmp);
1095         }
1096         return fd;
1097 }
1098
1099 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1100 {
1101         long ret;
1102
1103         if (force_o_largefile())
1104                 flags |= O_LARGEFILE;
1105
1106         ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1107         /* avoid REGPARM breakage on x86: */
1108         prevent_tail_call(ret);
1109         return ret;
1110 }
1111 EXPORT_SYMBOL_GPL(sys_open);
1112
1113 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1114                            int mode)
1115 {
1116         long ret;
1117
1118         if (force_o_largefile())
1119                 flags |= O_LARGEFILE;
1120
1121         ret = do_sys_open(dfd, filename, flags, mode);
1122         /* avoid REGPARM breakage on x86: */
1123         prevent_tail_call(ret);
1124         return ret;
1125 }
1126 EXPORT_SYMBOL_GPL(sys_openat);
1127
1128 #ifndef __alpha__
1129
1130 /*
1131  * For backward compatibility?  Maybe this should be moved
1132  * into arch/i386 instead?
1133  */
1134 asmlinkage long sys_creat(const char __user * pathname, int mode)
1135 {
1136         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1137 }
1138
1139 #endif
1140
1141 /*
1142  * "id" is the POSIX thread ID. We use the
1143  * files pointer for this..
1144  */
1145 int filp_close(struct file *filp, fl_owner_t id)
1146 {
1147         int retval = 0;
1148
1149         if (!file_count(filp)) {
1150                 printk(KERN_ERR "VFS: Close: file count is 0\n");
1151                 return 0;
1152         }
1153
1154         if (filp->f_op && filp->f_op->flush)
1155                 retval = filp->f_op->flush(filp);
1156
1157         dnotify_flush(filp, id);
1158         locks_remove_posix(filp, id);
1159         fput(filp);
1160         return retval;
1161 }
1162
1163 EXPORT_SYMBOL(filp_close);
1164
1165 /*
1166  * Careful here! We test whether the file pointer is NULL before
1167  * releasing the fd. This ensures that one clone task can't release
1168  * an fd while another clone is opening it.
1169  */
1170 asmlinkage long sys_close(unsigned int fd)
1171 {
1172         struct file * filp;
1173         struct files_struct *files = current->files;
1174         struct fdtable *fdt;
1175
1176         spin_lock(&files->file_lock);
1177         fdt = files_fdtable(files);
1178         if (fd >= fdt->max_fds)
1179                 goto out_unlock;
1180         filp = fdt->fd[fd];
1181         if (!filp)
1182                 goto out_unlock;
1183         rcu_assign_pointer(fdt->fd[fd], NULL);
1184         FD_CLR(fd, fdt->close_on_exec);
1185         __put_unused_fd(files, fd);
1186         spin_unlock(&files->file_lock);
1187         return filp_close(filp, files);
1188
1189 out_unlock:
1190         spin_unlock(&files->file_lock);
1191         return -EBADF;
1192 }
1193
1194 EXPORT_SYMBOL(sys_close);
1195
1196 /*
1197  * This routine simulates a hangup on the tty, to arrange that users
1198  * are given clean terminals at login time.
1199  */
1200 asmlinkage long sys_vhangup(void)
1201 {
1202         if (capable(CAP_SYS_TTY_CONFIG)) {
1203                 tty_vhangup(current->signal->tty);
1204                 return 0;
1205         }
1206         return -EPERM;
1207 }
1208
1209 /*
1210  * Called when an inode is about to be open.
1211  * We use this to disallow opening large files on 32bit systems if
1212  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1213  * on this flag in sys_open.
1214  */
1215 int generic_file_open(struct inode * inode, struct file * filp)
1216 {
1217         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1218                 return -EFBIG;
1219         return 0;
1220 }
1221
1222 EXPORT_SYMBOL(generic_file_open);
1223
1224 /*
1225  * This is used by subsystems that don't want seekable
1226  * file descriptors
1227  */
1228 int nonseekable_open(struct inode *inode, struct file *filp)
1229 {
1230         filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1231         return 0;
1232 }
1233
1234 EXPORT_SYMBOL(nonseekable_open);