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