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