- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.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/file.h>
10 #include <linux/fdtable.h>
11 #include <linux/quotaops.h>
12 #include <linux/fsnotify.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/namei.h>
17 #include <linux/backing-dev.h>
18 #include <linux/capability.h>
19 #include <linux/securebits.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/falloc.h>
32 #include <linux/fs_struct.h>
33 #include <linux/ima.h>
34 #include <trace/fs.h>
35
36 #include "internal.h"
37
38 DEFINE_TRACE(fs_open);
39 DEFINE_TRACE(fs_close);
40
41 int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
42 {
43         int retval = -ENODEV;
44
45         if (dentry) {
46                 retval = -ENOSYS;
47                 if (dentry->d_sb->s_op->statfs) {
48                         memset(buf, 0, sizeof(*buf));
49                         retval = security_sb_statfs(dentry);
50                         if (retval)
51                                 return retval;
52                         retval = dentry->d_sb->s_op->statfs(dentry, buf);
53                         if (retval == 0 && buf->f_frsize == 0)
54                                 buf->f_frsize = buf->f_bsize;
55                 }
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                              st.f_bsize | st.f_frsize) &
77                             0xffffffff00000000ULL)
78                                 return -EOVERFLOW;
79                         /*
80                          * f_files and f_ffree may be -1; it's okay to stuff
81                          * that into 32 bits
82                          */
83                         if (st.f_files != -1 &&
84                             (st.f_files & 0xffffffff00000000ULL))
85                                 return -EOVERFLOW;
86                         if (st.f_ffree != -1 &&
87                             (st.f_ffree & 0xffffffff00000000ULL))
88                                 return -EOVERFLOW;
89                 }
90
91                 buf->f_type = st.f_type;
92                 buf->f_bsize = st.f_bsize;
93                 buf->f_blocks = st.f_blocks;
94                 buf->f_bfree = st.f_bfree;
95                 buf->f_bavail = st.f_bavail;
96                 buf->f_files = st.f_files;
97                 buf->f_ffree = st.f_ffree;
98                 buf->f_fsid = st.f_fsid;
99                 buf->f_namelen = st.f_namelen;
100                 buf->f_frsize = st.f_frsize;
101                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
102         }
103         return 0;
104 }
105
106 static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
107 {
108         struct kstatfs st;
109         int retval;
110
111         retval = vfs_statfs(dentry, &st);
112         if (retval)
113                 return retval;
114
115         if (sizeof(*buf) == sizeof(st))
116                 memcpy(buf, &st, sizeof(st));
117         else {
118                 buf->f_type = st.f_type;
119                 buf->f_bsize = st.f_bsize;
120                 buf->f_blocks = st.f_blocks;
121                 buf->f_bfree = st.f_bfree;
122                 buf->f_bavail = st.f_bavail;
123                 buf->f_files = st.f_files;
124                 buf->f_ffree = st.f_ffree;
125                 buf->f_fsid = st.f_fsid;
126                 buf->f_namelen = st.f_namelen;
127                 buf->f_frsize = st.f_frsize;
128                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
129         }
130         return 0;
131 }
132
133 SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
134 {
135         struct path path;
136         int error;
137
138         error = user_path(pathname, &path);
139         if (!error) {
140                 struct statfs tmp;
141                 error = vfs_statfs_native(path.dentry, &tmp);
142                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
143                         error = -EFAULT;
144                 path_put(&path);
145         }
146         return error;
147 }
148
149 SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
150 {
151         struct path path;
152         long error;
153
154         if (sz != sizeof(*buf))
155                 return -EINVAL;
156         error = user_path(pathname, &path);
157         if (!error) {
158                 struct statfs64 tmp;
159                 error = vfs_statfs64(path.dentry, &tmp);
160                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
161                         error = -EFAULT;
162                 path_put(&path);
163         }
164         return error;
165 }
166
167 SYSCALL_DEFINE2(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_path.dentry, &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 SYSCALL_DEFINE3(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_path.dentry, &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 ret;
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         /* Remove suid/sgid on truncate too */
224         ret = should_remove_suid(dentry);
225         if (ret)
226                 newattrs.ia_valid |= ret | ATTR_FORCE;
227
228         mutex_lock(&dentry->d_inode->i_mutex);
229         ret = notify_change(dentry, &newattrs);
230         mutex_unlock(&dentry->d_inode->i_mutex);
231         return ret;
232 }
233
234 static long do_sys_truncate(const char __user *pathname, loff_t length)
235 {
236         struct path path;
237         struct inode *inode;
238         int error;
239
240         error = -EINVAL;
241         if (length < 0) /* sorry, but loff_t says... */
242                 goto out;
243
244         error = user_path(pathname, &path);
245         if (error)
246                 goto out;
247         inode = path.dentry->d_inode;
248
249         /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
250         error = -EISDIR;
251         if (S_ISDIR(inode->i_mode))
252                 goto dput_and_out;
253
254         error = -EINVAL;
255         if (!S_ISREG(inode->i_mode))
256                 goto dput_and_out;
257
258         error = mnt_want_write(path.mnt);
259         if (error)
260                 goto dput_and_out;
261
262         error = inode_permission(inode, MAY_WRITE);
263         if (error)
264                 goto mnt_drop_write_and_out;
265
266         error = -EPERM;
267         if (IS_APPEND(inode))
268                 goto mnt_drop_write_and_out;
269
270         error = get_write_access(inode);
271         if (error)
272                 goto mnt_drop_write_and_out;
273
274         /*
275          * Make sure that there are no leases.  get_write_access() protects
276          * against the truncate racing with a lease-granting setlease().
277          */
278         error = break_lease(inode, FMODE_WRITE);
279         if (error)
280                 goto put_write_and_out;
281
282         error = locks_verify_truncate(inode, NULL, length);
283         if (!error)
284                 error = security_path_truncate(&path, length, 0);
285         if (!error) {
286                 vfs_dq_init(inode);
287                 error = do_truncate(path.dentry, length, 0, NULL);
288         }
289
290 put_write_and_out:
291         put_write_access(inode);
292 mnt_drop_write_and_out:
293         mnt_drop_write(path.mnt);
294 dput_and_out:
295         path_put(&path);
296 out:
297         return error;
298 }
299
300 SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
301 {
302         return do_sys_truncate(path, length);
303 }
304
305 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
306 {
307         struct inode * inode;
308         struct dentry *dentry;
309         struct file * file;
310         int error;
311
312         error = -EINVAL;
313         if (length < 0)
314                 goto out;
315         error = -EBADF;
316         file = fget(fd);
317         if (!file)
318                 goto out;
319
320         /* explicitly opened as large or we are on 64-bit box */
321         if (file->f_flags & O_LARGEFILE)
322                 small = 0;
323
324         dentry = file->f_path.dentry;
325         inode = dentry->d_inode;
326         error = -EINVAL;
327         if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
328                 goto out_putf;
329
330         error = -EINVAL;
331         /* Cannot ftruncate over 2^31 bytes without large file support */
332         if (small && length > MAX_NON_LFS)
333                 goto out_putf;
334
335         error = -EPERM;
336         if (IS_APPEND(inode))
337                 goto out_putf;
338
339         error = locks_verify_truncate(inode, file, length);
340         if (!error)
341                 error = security_path_truncate(&file->f_path, length,
342                                                ATTR_MTIME|ATTR_CTIME);
343         if (!error)
344                 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
345 out_putf:
346         fput(file);
347 out:
348         return error;
349 }
350
351 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
352 {
353         long ret = do_sys_ftruncate(fd, length, 1);
354         /* avoid REGPARM breakage on x86: */
355         asmlinkage_protect(2, ret, fd, length);
356         return ret;
357 }
358
359 /* LFS versions of truncate are only needed on 32 bit machines */
360 #if BITS_PER_LONG == 32
361 SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
362 {
363         return do_sys_truncate(path, length);
364 }
365 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
366 asmlinkage long SyS_truncate64(long path, loff_t length)
367 {
368         return SYSC_truncate64((const char __user *) path, length);
369 }
370 SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
371 #endif
372
373 SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
374 {
375         long ret = do_sys_ftruncate(fd, length, 0);
376         /* avoid REGPARM breakage on x86: */
377         asmlinkage_protect(2, ret, fd, length);
378         return ret;
379 }
380 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
381 asmlinkage long SyS_ftruncate64(long fd, loff_t length)
382 {
383         return SYSC_ftruncate64((unsigned int) fd, length);
384 }
385 SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
386 #endif
387 #endif /* BITS_PER_LONG == 32 */
388
389
390 int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
391 {
392         struct inode *inode = file->f_path.dentry->d_inode;
393         long ret;
394
395         if (offset < 0 || len <= 0)
396                 return -EINVAL;
397
398         /* Return error if mode is not supported */
399         if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
400                 return -EOPNOTSUPP;
401
402         if (!(file->f_mode & FMODE_WRITE))
403                 return -EBADF;
404         /*
405          * Revalidate the write permissions, in case security policy has
406          * changed since the files were opened.
407          */
408         ret = security_file_permission(file, MAY_WRITE);
409         if (ret)
410                 return ret;
411
412         if (S_ISFIFO(inode->i_mode))
413                 return -ESPIPE;
414
415         /*
416          * Let individual file system decide if it supports preallocation
417          * for directories or not.
418          */
419         if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
420                 return -ENODEV;
421
422         /* Check for wrap through zero too */
423         if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
424                 return -EFBIG;
425
426         if (!inode->i_op->fallocate)
427                 return -EOPNOTSUPP;
428
429         return inode->i_op->fallocate(inode, mode, offset, len);
430 }
431
432 SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
433 {
434         struct file *file;
435         int error = -EBADF;
436
437         file = fget(fd);
438         if (file) {
439                 error = do_fallocate(file, mode, offset, len);
440                 fput(file);
441         }
442
443         return error;
444 }
445
446 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
447 asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
448 {
449         return SYSC_fallocate((int)fd, (int)mode, offset, len);
450 }
451 SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
452 #endif
453
454 /*
455  * access() needs to use the real uid/gid, not the effective uid/gid.
456  * We do this by temporarily clearing all FS-related capabilities and
457  * switching the fsuid/fsgid around to the real ones.
458  */
459 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
460 {
461         const struct cred *old_cred;
462         struct cred *override_cred;
463         struct path path;
464         struct inode *inode;
465         int res;
466
467         if (mode & ~S_IRWXO)    /* where's F_OK, X_OK, W_OK, R_OK? */
468                 return -EINVAL;
469
470         override_cred = prepare_creds();
471         if (!override_cred)
472                 return -ENOMEM;
473
474         override_cred->fsuid = override_cred->uid;
475         override_cred->fsgid = override_cred->gid;
476
477         if (!issecure(SECURE_NO_SETUID_FIXUP)) {
478                 /* Clear the capabilities if we switch to a non-root user */
479                 if (override_cred->uid)
480                         cap_clear(override_cred->cap_effective);
481                 else
482                         override_cred->cap_effective =
483                                 override_cred->cap_permitted;
484         }
485
486         old_cred = override_creds(override_cred);
487
488         res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
489         if (res)
490                 goto out;
491
492         inode = path.dentry->d_inode;
493
494         if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
495                 /*
496                  * MAY_EXEC on regular files is denied if the fs is mounted
497                  * with the "noexec" flag.
498                  */
499                 res = -EACCES;
500                 if (path.mnt->mnt_flags & MNT_NOEXEC)
501                         goto out_path_release;
502         }
503
504         res = inode_permission(inode, mode | MAY_ACCESS);
505         /* SuS v2 requires we report a read only fs too */
506         if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
507                 goto out_path_release;
508         /*
509          * This is a rare case where using __mnt_is_readonly()
510          * is OK without a mnt_want/drop_write() pair.  Since
511          * no actual write to the fs is performed here, we do
512          * not need to telegraph to that to anyone.
513          *
514          * By doing this, we accept that this access is
515          * inherently racy and know that the fs may change
516          * state before we even see this result.
517          */
518         if (__mnt_is_readonly(path.mnt))
519                 res = -EROFS;
520
521 out_path_release:
522         path_put(&path);
523 out:
524         revert_creds(old_cred);
525         put_cred(override_cred);
526         return res;
527 }
528
529 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
530 {
531         return sys_faccessat(AT_FDCWD, filename, mode);
532 }
533
534 SYSCALL_DEFINE1(chdir, const char __user *, filename)
535 {
536         struct path path;
537         int error;
538
539         error = user_path_dir(filename, &path);
540         if (error)
541                 goto out;
542
543         error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
544         if (error)
545                 goto dput_and_out;
546
547         set_fs_pwd(current->fs, &path);
548
549 dput_and_out:
550         path_put(&path);
551 out:
552         return error;
553 }
554
555 SYSCALL_DEFINE1(fchdir, unsigned int, fd)
556 {
557         struct file *file;
558         struct inode *inode;
559         int error;
560
561         error = -EBADF;
562         file = fget(fd);
563         if (!file)
564                 goto out;
565
566         inode = file->f_path.dentry->d_inode;
567
568         error = -ENOTDIR;
569         if (!S_ISDIR(inode->i_mode))
570                 goto out_putf;
571
572         error = inode_permission(inode, MAY_EXEC | MAY_ACCESS);
573         if (!error)
574                 set_fs_pwd(current->fs, &file->f_path);
575 out_putf:
576         fput(file);
577 out:
578         return error;
579 }
580
581 SYSCALL_DEFINE1(chroot, const char __user *, filename)
582 {
583         struct path path;
584         int error;
585
586         error = user_path_dir(filename, &path);
587         if (error)
588                 goto out;
589
590         error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
591         if (error)
592                 goto dput_and_out;
593
594         error = -EPERM;
595         if (!capable(CAP_SYS_CHROOT))
596                 goto dput_and_out;
597         error = security_path_chroot(&path);
598         if (error)
599                 goto dput_and_out;
600
601         set_fs_root(current->fs, &path);
602         error = 0;
603 dput_and_out:
604         path_put(&path);
605 out:
606         return error;
607 }
608
609 SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
610 {
611         struct inode * inode;
612         struct dentry * dentry;
613         struct file * file;
614         int err = -EBADF;
615         struct iattr newattrs;
616
617         file = fget(fd);
618         if (!file)
619                 goto out;
620
621         dentry = file->f_path.dentry;
622         inode = dentry->d_inode;
623
624         audit_inode(NULL, dentry);
625
626         err = mnt_want_write_file(file);
627         if (err)
628                 goto out_putf;
629         mutex_lock(&inode->i_mutex);
630         err = security_path_chmod(dentry, file->f_vfsmnt, mode);
631         if (err)
632                 goto out_unlock;
633         if (mode == (mode_t) -1)
634                 mode = inode->i_mode;
635         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
636         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
637         err = notify_change(dentry, &newattrs);
638 out_unlock:
639         mutex_unlock(&inode->i_mutex);
640         mnt_drop_write(file->f_path.mnt);
641 out_putf:
642         fput(file);
643 out:
644         return err;
645 }
646
647 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
648 {
649         struct path path;
650         struct inode *inode;
651         int error;
652         struct iattr newattrs;
653
654         error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
655         if (error)
656                 goto out;
657         inode = path.dentry->d_inode;
658
659         error = mnt_want_write(path.mnt);
660         if (error)
661                 goto dput_and_out;
662         mutex_lock(&inode->i_mutex);
663         error = security_path_chmod(path.dentry, path.mnt, mode);
664         if (error)
665                 goto out_unlock;
666         if (mode == (mode_t) -1)
667                 mode = inode->i_mode;
668         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
669         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
670         error = notify_change(path.dentry, &newattrs);
671 out_unlock:
672         mutex_unlock(&inode->i_mutex);
673         mnt_drop_write(path.mnt);
674 dput_and_out:
675         path_put(&path);
676 out:
677         return error;
678 }
679
680 SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
681 {
682         return sys_fchmodat(AT_FDCWD, filename, mode);
683 }
684
685 static int chown_common(struct path *path, uid_t user, gid_t group)
686 {
687         struct inode *inode = path->dentry->d_inode;
688         int error;
689         struct iattr newattrs;
690
691         newattrs.ia_valid =  ATTR_CTIME;
692         if (user != (uid_t) -1) {
693                 newattrs.ia_valid |= ATTR_UID;
694                 newattrs.ia_uid = user;
695         }
696         if (group != (gid_t) -1) {
697                 newattrs.ia_valid |= ATTR_GID;
698                 newattrs.ia_gid = group;
699         }
700         if (!S_ISDIR(inode->i_mode))
701                 newattrs.ia_valid |=
702                         ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
703         mutex_lock(&inode->i_mutex);
704         error = security_path_chown(path, user, group);
705         if (!error)
706                 error = notify_change(path->dentry, &newattrs);
707         mutex_unlock(&inode->i_mutex);
708
709         return error;
710 }
711
712 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
713 {
714         struct path path;
715         int error;
716
717         error = user_path(filename, &path);
718         if (error)
719                 goto out;
720         error = mnt_want_write(path.mnt);
721         if (error)
722                 goto out_release;
723         error = chown_common(&path, user, group);
724         mnt_drop_write(path.mnt);
725 out_release:
726         path_put(&path);
727 out:
728         return error;
729 }
730
731 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
732                 gid_t, group, int, flag)
733 {
734         struct path path;
735         int error = -EINVAL;
736         int follow;
737
738         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
739                 goto out;
740
741         follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
742         error = user_path_at(dfd, filename, follow, &path);
743         if (error)
744                 goto out;
745         error = mnt_want_write(path.mnt);
746         if (error)
747                 goto out_release;
748         error = chown_common(&path, user, group);
749         mnt_drop_write(path.mnt);
750 out_release:
751         path_put(&path);
752 out:
753         return error;
754 }
755
756 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
757 {
758         struct path path;
759         int error;
760
761         error = user_lpath(filename, &path);
762         if (error)
763                 goto out;
764         error = mnt_want_write(path.mnt);
765         if (error)
766                 goto out_release;
767         error = chown_common(&path, user, group);
768         mnt_drop_write(path.mnt);
769 out_release:
770         path_put(&path);
771 out:
772         return error;
773 }
774
775 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
776 {
777         struct file * file;
778         int error = -EBADF;
779         struct dentry * dentry;
780
781         file = fget(fd);
782         if (!file)
783                 goto out;
784
785         error = mnt_want_write_file(file);
786         if (error)
787                 goto out_fput;
788         dentry = file->f_path.dentry;
789         audit_inode(NULL, dentry);
790         error = chown_common(&file->f_path, user, group);
791         mnt_drop_write(file->f_path.mnt);
792 out_fput:
793         fput(file);
794 out:
795         return error;
796 }
797
798 /*
799  * You have to be very careful that these write
800  * counts get cleaned up in error cases and
801  * upon __fput().  This should probably never
802  * be called outside of __dentry_open().
803  */
804 static inline int __get_file_write_access(struct inode *inode,
805                                           struct vfsmount *mnt)
806 {
807         int error;
808         error = get_write_access(inode);
809         if (error)
810                 return error;
811         /*
812          * Do not take mount writer counts on
813          * special files since no writes to
814          * the mount itself will occur.
815          */
816         if (!special_file(inode->i_mode)) {
817                 /*
818                  * Balanced in __fput()
819                  */
820                 error = mnt_want_write(mnt);
821                 if (error)
822                         put_write_access(inode);
823         }
824         return error;
825 }
826
827 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
828                                         struct file *f,
829                                         int (*open)(struct inode *, struct file *),
830                                         const struct cred *cred)
831 {
832         struct inode *inode;
833         int error;
834
835         f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
836                                 FMODE_PREAD | FMODE_PWRITE;
837         inode = dentry->d_inode;
838         if (f->f_mode & FMODE_WRITE) {
839                 error = __get_file_write_access(inode, mnt);
840                 if (error)
841                         goto cleanup_file;
842                 if (!special_file(inode->i_mode))
843                         file_take_write(f);
844         }
845
846         f->f_mapping = inode->i_mapping;
847         f->f_path.dentry = dentry;
848         f->f_path.mnt = mnt;
849         f->f_pos = 0;
850         f->f_op = fops_get(inode->i_fop);
851         file_move(f, &inode->i_sb->s_files);
852
853         error = security_dentry_open(f, cred);
854         if (error)
855                 goto cleanup_all;
856
857         if (!open && f->f_op)
858                 open = f->f_op->open;
859         if (open) {
860                 error = open(inode, f);
861                 if (error)
862                         goto cleanup_all;
863         }
864         ima_counts_get(f);
865
866         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
867
868         file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
869
870         /* NB: we're sure to have correct a_ops only after f_op->open */
871         if (f->f_flags & O_DIRECT) {
872                 if (!f->f_mapping->a_ops ||
873                     ((!f->f_mapping->a_ops->direct_IO) &&
874                     (!f->f_mapping->a_ops->get_xip_mem))) {
875                         fput(f);
876                         f = ERR_PTR(-EINVAL);
877                 }
878         }
879
880         return f;
881
882 cleanup_all:
883         fops_put(f->f_op);
884         if (f->f_mode & FMODE_WRITE) {
885                 put_write_access(inode);
886                 if (!special_file(inode->i_mode)) {
887                         /*
888                          * We don't consider this a real
889                          * mnt_want/drop_write() pair
890                          * because it all happenend right
891                          * here, so just reset the state.
892                          */
893                         file_reset_write(f);
894                         mnt_drop_write(mnt);
895                 }
896         }
897         file_kill(f);
898         f->f_path.dentry = NULL;
899         f->f_path.mnt = NULL;
900 cleanup_file:
901         put_filp(f);
902         dput(dentry);
903         mntput(mnt);
904         return ERR_PTR(error);
905 }
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         const struct cred *cred = current_cred();
930
931         if (IS_ERR(nd->intent.open.file))
932                 goto out;
933         if (IS_ERR(dentry))
934                 goto out_err;
935         nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
936                                              nd->intent.open.file,
937                                              open, cred);
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)
955 {
956         const struct cred *cred = current_cred();
957         struct file *filp;
958
959         /* Pick up the filp from the open intent */
960         filp = nd->intent.open.file;
961         /* Has the filesystem initialised the file for us? */
962         if (filp->f_path.dentry == NULL)
963                 filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
964                                      NULL, cred);
965         else
966                 path_put(&nd->path);
967         return filp;
968 }
969
970 /*
971  * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
972  * error.
973  */
974 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
975                          const struct cred *cred)
976 {
977         int error;
978         struct file *f;
979
980         validate_creds(cred);
981
982         /*
983          * We must always pass in a valid mount pointer.   Historically
984          * callers got away with not passing it, but we must enforce this at
985          * the earliest possible point now to avoid strange problems deep in the
986          * filesystem stack.
987          */
988         if (!mnt) {
989                 printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
990                 dump_stack();
991                 return ERR_PTR(-EINVAL);
992         }
993
994         error = -ENFILE;
995         f = get_empty_filp();
996         if (f == NULL) {
997                 dput(dentry);
998                 mntput(mnt);
999                 return ERR_PTR(error);
1000         }
1001
1002         f->f_flags = flags;
1003         return __dentry_open(dentry, mnt, f, NULL, cred);
1004 }
1005 EXPORT_SYMBOL(dentry_open);
1006
1007 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1008 {
1009         struct fdtable *fdt = files_fdtable(files);
1010         __FD_CLR(fd, fdt->open_fds);
1011         if (fd < files->next_fd)
1012                 files->next_fd = fd;
1013 }
1014
1015 void put_unused_fd(unsigned int fd)
1016 {
1017         struct files_struct *files = current->files;
1018         spin_lock(&files->file_lock);
1019         __put_unused_fd(files, fd);
1020         spin_unlock(&files->file_lock);
1021 }
1022
1023 EXPORT_SYMBOL(put_unused_fd);
1024
1025 /*
1026  * Install a file pointer in the fd array.
1027  *
1028  * The VFS is full of places where we drop the files lock between
1029  * setting the open_fds bitmap and installing the file in the file
1030  * array.  At any such point, we are vulnerable to a dup2() race
1031  * installing a file in the array before us.  We need to detect this and
1032  * fput() the struct file we are about to overwrite in this case.
1033  *
1034  * It should never happen - if we allow dup2() do it, _really_ bad things
1035  * will follow.
1036  */
1037
1038 void fd_install(unsigned int fd, struct file *file)
1039 {
1040         struct files_struct *files = current->files;
1041         struct fdtable *fdt;
1042         spin_lock(&files->file_lock);
1043         fdt = files_fdtable(files);
1044         BUG_ON(fdt->fd[fd] != NULL);
1045         rcu_assign_pointer(fdt->fd[fd], file);
1046         spin_unlock(&files->file_lock);
1047 }
1048
1049 EXPORT_SYMBOL(fd_install);
1050
1051 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1052 {
1053         char *tmp = getname(filename);
1054         int fd = PTR_ERR(tmp);
1055
1056         if (!IS_ERR(tmp)) {
1057                 fd = get_unused_fd_flags(flags);
1058                 if (fd >= 0) {
1059                         struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
1060                         if (IS_ERR(f)) {
1061                                 put_unused_fd(fd);
1062                                 fd = PTR_ERR(f);
1063                         } else {
1064                                 fsnotify_open(f->f_path.dentry);
1065                                 fd_install(fd, f);
1066                         }
1067                         trace_fs_open(fd, tmp);
1068                 }
1069                 putname(tmp);
1070         }
1071         return fd;
1072 }
1073
1074 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
1075 {
1076         long ret;
1077
1078         if (force_o_largefile())
1079                 flags |= O_LARGEFILE;
1080
1081         ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1082         /* avoid REGPARM breakage on x86: */
1083         asmlinkage_protect(3, ret, filename, flags, mode);
1084         return ret;
1085 }
1086
1087 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
1088                 int, mode)
1089 {
1090         long ret;
1091
1092         if (force_o_largefile())
1093                 flags |= O_LARGEFILE;
1094
1095         ret = do_sys_open(dfd, filename, flags, mode);
1096         /* avoid REGPARM breakage on x86: */
1097         asmlinkage_protect(4, ret, dfd, filename, flags, mode);
1098         return ret;
1099 }
1100
1101 #ifndef __alpha__
1102
1103 /*
1104  * For backward compatibility?  Maybe this should be moved
1105  * into arch/i386 instead?
1106  */
1107 SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
1108 {
1109         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1110 }
1111
1112 #endif
1113
1114 /*
1115  * "id" is the POSIX thread ID. We use the
1116  * files pointer for this..
1117  */
1118 int filp_close(struct file *filp, fl_owner_t id)
1119 {
1120         int retval = 0;
1121
1122         if (unlikely(!filp->f_count)) {
1123                 printk(KERN_ERR "VFS: Close: file count is 0\n");
1124                 return 0;
1125         }
1126
1127         if (filp->f_op && filp->f_op->flush)
1128                 retval = filp->f_op->flush(filp, id);
1129
1130         dnotify_flush(filp, id);
1131         locks_remove_posix(filp, id);
1132         fput(filp);
1133         return retval;
1134 }
1135
1136 EXPORT_SYMBOL(filp_close);
1137
1138 /*
1139  * Careful here! We test whether the file pointer is NULL before
1140  * releasing the fd. This ensures that one clone task can't release
1141  * an fd while another clone is opening it.
1142  */
1143 SYSCALL_DEFINE1(close, unsigned int, fd)
1144 {
1145         struct file * filp;
1146         struct files_struct *files = current->files;
1147         struct fdtable *fdt;
1148         int retval;
1149
1150         spin_lock(&files->file_lock);
1151         fdt = files_fdtable(files);
1152         if (fd >= fdt->max_fds)
1153                 goto out_unlock;
1154         filp = fdt->fd[fd];
1155         if (!filp)
1156                 goto out_unlock;
1157         trace_fs_close(fd);
1158         rcu_assign_pointer(fdt->fd[fd], NULL);
1159         FD_CLR(fd, fdt->close_on_exec);
1160         __put_unused_fd(files, fd);
1161         spin_unlock(&files->file_lock);
1162         retval = filp_close(filp, files);
1163
1164         /* can't restart close syscall because file table entry was cleared */
1165         if (unlikely(retval == -ERESTARTSYS ||
1166                      retval == -ERESTARTNOINTR ||
1167                      retval == -ERESTARTNOHAND ||
1168                      retval == -ERESTART_RESTARTBLOCK))
1169                 retval = -EINTR;
1170
1171         return retval;
1172
1173 out_unlock:
1174         spin_unlock(&files->file_lock);
1175         return -EBADF;
1176 }
1177 EXPORT_SYMBOL(sys_close);
1178
1179 /*
1180  * This routine simulates a hangup on the tty, to arrange that users
1181  * are given clean terminals at login time.
1182  */
1183 SYSCALL_DEFINE0(vhangup)
1184 {
1185         if (capable(CAP_SYS_TTY_CONFIG)) {
1186                 tty_vhangup_self();
1187                 return 0;
1188         }
1189         return -EPERM;
1190 }
1191
1192 /*
1193  * Called when an inode is about to be open.
1194  * We use this to disallow opening large files on 32bit systems if
1195  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1196  * on this flag in sys_open.
1197  */
1198 int generic_file_open(struct inode * inode, struct file * filp)
1199 {
1200         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1201                 return -EOVERFLOW;
1202         return 0;
1203 }
1204
1205 EXPORT_SYMBOL(generic_file_open);
1206
1207 /*
1208  * This is used by subsystems that don't want seekable
1209  * file descriptors
1210  */
1211 int nonseekable_open(struct inode *inode, struct file *filp)
1212 {
1213         filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1214         return 0;
1215 }
1216
1217 EXPORT_SYMBOL(nonseekable_open);