Blame view

fs/namei.c 111 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   *  linux/fs/namei.c
   *
   *  Copyright (C) 1991, 1992  Linus Torvalds
   */
  
  /*
   * Some corrections by tytso.
   */
  
  /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
   * lookup logic.
   */
  /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
   */
  
  #include <linux/init.h>
630d9c472   Paul Gortmaker   fs: reduce the us...
18
  #include <linux/export.h>
446969084   David S. Miller   kernel: Move REPE...
19
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include <linux/namei.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
  #include <linux/pagemap.h>
0eeca2830   Robert Love   [PATCH] inotify
24
  #include <linux/fsnotify.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
  #include <linux/personality.h>
  #include <linux/security.h>
6146f0d5e   Mimi Zohar   integrity: IMA hooks
27
  #include <linux/ima.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
  #include <linux/syscalls.h>
  #include <linux/mount.h>
  #include <linux/audit.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
31
  #include <linux/capability.h>
834f2a4a1   Trond Myklebust   VFS: Allow the fi...
32
  #include <linux/file.h>
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
33
  #include <linux/fcntl.h>
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
34
  #include <linux/device_cgroup.h>
5ad4e53bd   Al Viro   Get rid of indire...
35
  #include <linux/fs_struct.h>
e77819e57   Linus Torvalds   vfs: move ACL cac...
36
  #include <linux/posix_acl.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  #include <asm/uaccess.h>
e81e3f4dc   Eric Paris   fs: move get_empt...
38
  #include "internal.h"
c71053659   Al Viro   vfs: spread struc...
39
  #include "mount.h"
e81e3f4dc   Eric Paris   fs: move get_empt...
40

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  /* [Feb-1997 T. Schoebel-Theuer]
   * Fundamental changes in the pathname lookup mechanisms (namei)
   * were necessary because of omirr.  The reason is that omirr needs
   * to know the _real_ pathname, not the user-supplied one, in case
   * of symlinks (and also when transname replacements occur).
   *
   * The new code replaces the old recursive symlink resolution with
   * an iterative one (in case of non-nested symlink chains).  It does
   * this with calls to <fs>_follow_link().
   * As a side effect, dir_namei(), _namei() and follow_link() are now 
   * replaced with a single function lookup_dentry() that can handle all 
   * the special cases of the former code.
   *
   * With the new dcache, the pathname is stored at each inode, at least as
   * long as the refcount of the inode is positive.  As a side effect, the
   * size of the dcache depends on the inode cache and thus is dynamic.
   *
   * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
   * resolution to correspond with current state of the code.
   *
   * Note that the symlink resolution is not *completely* iterative.
   * There is still a significant amount of tail- and mid- recursion in
   * the algorithm.  Also, note that <fs>_readlink() is not used in
   * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
   * may return different results than <fs>_follow_link().  Many virtual
   * filesystems (including /proc) exhibit this behavior.
   */
  
  /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
   * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
   * and the name already exists in form of a symlink, try to create the new
   * name indicated by the symlink. The old code always complained that the
   * name already exists, due to not following the symlink even if its target
   * is nonexistent.  The new semantics affects also mknod() and link() when
25985edce   Lucas De Marchi   Fix common misspe...
75
   * the name is a symlink pointing to a non-existent name.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
   *
   * I don't know which semantics is the right one, since I have no access
   * to standards. But I found by trial that HP-UX 9.0 has the full "new"
   * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
   * "old" one. Personally, I think the new semantics is much more logical.
   * Note that "ln old new" where "new" is a symlink pointing to a non-existing
   * file does succeed in both HP-UX and SunOs, but not in Solaris
   * and in the old Linux semantics.
   */
  
  /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
   * semantics.  See the comments in "open_namei" and "do_link" below.
   *
   * [10-Sep-98 Alan Modra] Another symlink change.
   */
  
  /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
   *	inside the path - always follow.
   *	in the last component in creation/removal/renaming - never follow.
   *	if LOOKUP_FOLLOW passed - follow.
   *	if the pathname has trailing slashes - follow.
   *	otherwise - don't follow.
   * (applied in that order).
   *
   * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
   * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
   * During the 2.4 we need to fix the userland stuff depending on it -
   * hopefully we will be able to get rid of that wart in 2.5. So far only
   * XEmacs seems to be relying on it...
   */
  /*
   * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
108
   * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
113
114
115
116
117
118
   * any extra contention...
   */
  
  /* In order to reduce some races, while at the same time doing additional
   * checking and hopefully speeding things up, we copy filenames to the
   * kernel data space before using them..
   *
   * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
   * PATH_MAX includes the nul terminator --RR.
   */
91a27b2a7   Jeff Layton   vfs: define struc...
119
  void final_putname(struct filename *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
  {
7950e3852   Jeff Layton   vfs: embed struct...
121
122
123
124
125
126
  	if (name->separate) {
  		__putname(name->name);
  		kfree(name);
  	} else {
  		__putname(name);
  	}
91a27b2a7   Jeff Layton   vfs: define struc...
127
  }
7950e3852   Jeff Layton   vfs: embed struct...
128
  #define EMBEDDED_NAME_MAX	(PATH_MAX - sizeof(struct filename))
91a27b2a7   Jeff Layton   vfs: define struc...
129
130
131
132
  static struct filename *
  getname_flags(const char __user *filename, int flags, int *empty)
  {
  	struct filename *result, *err;
3f9f0aa68   Linus Torvalds   VFS: clean up and...
133
  	int len;
7950e3852   Jeff Layton   vfs: embed struct...
134
135
  	long max;
  	char *kname;
4043cde8e   Eric Paris   audit: do not cal...
136

7ac86265d   Jeff Layton   audit: allow audi...
137
138
139
  	result = audit_reusename(filename);
  	if (result)
  		return result;
7950e3852   Jeff Layton   vfs: embed struct...
140
  	result = __getname();
3f9f0aa68   Linus Torvalds   VFS: clean up and...
141
  	if (unlikely(!result))
4043cde8e   Eric Paris   audit: do not cal...
142
  		return ERR_PTR(-ENOMEM);
7950e3852   Jeff Layton   vfs: embed struct...
143
144
145
146
147
  	/*
  	 * First, try to embed the struct filename inside the names_cache
  	 * allocation
  	 */
  	kname = (char *)result + sizeof(*result);
91a27b2a7   Jeff Layton   vfs: define struc...
148
  	result->name = kname;
7950e3852   Jeff Layton   vfs: embed struct...
149
150
151
152
153
  	result->separate = false;
  	max = EMBEDDED_NAME_MAX;
  
  recopy:
  	len = strncpy_from_user(kname, filename, max);
91a27b2a7   Jeff Layton   vfs: define struc...
154
155
  	if (unlikely(len < 0)) {
  		err = ERR_PTR(len);
3f9f0aa68   Linus Torvalds   VFS: clean up and...
156
  		goto error;
91a27b2a7   Jeff Layton   vfs: define struc...
157
  	}
3f9f0aa68   Linus Torvalds   VFS: clean up and...
158

7950e3852   Jeff Layton   vfs: embed struct...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  	/*
  	 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
  	 * separate struct filename so we can dedicate the entire
  	 * names_cache allocation for the pathname, and re-do the copy from
  	 * userland.
  	 */
  	if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
  		kname = (char *)result;
  
  		result = kzalloc(sizeof(*result), GFP_KERNEL);
  		if (!result) {
  			err = ERR_PTR(-ENOMEM);
  			result = (struct filename *)kname;
  			goto error;
  		}
  		result->name = kname;
  		result->separate = true;
  		max = PATH_MAX;
  		goto recopy;
  	}
3f9f0aa68   Linus Torvalds   VFS: clean up and...
179
180
181
  	/* The empty path is special. */
  	if (unlikely(!len)) {
  		if (empty)
4043cde8e   Eric Paris   audit: do not cal...
182
  			*empty = 1;
3f9f0aa68   Linus Torvalds   VFS: clean up and...
183
184
185
  		err = ERR_PTR(-ENOENT);
  		if (!(flags & LOOKUP_EMPTY))
  			goto error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
  	}
3f9f0aa68   Linus Torvalds   VFS: clean up and...
187
188
  
  	err = ERR_PTR(-ENAMETOOLONG);
7950e3852   Jeff Layton   vfs: embed struct...
189
190
191
192
  	if (unlikely(len >= PATH_MAX))
  		goto error;
  
  	result->uptr = filename;
c4ad8f98b   Linus Torvalds   execve: use 'stru...
193
  	result->aname = NULL;
7950e3852   Jeff Layton   vfs: embed struct...
194
195
  	audit_getname(result);
  	return result;
3f9f0aa68   Linus Torvalds   VFS: clean up and...
196
197
  
  error:
7950e3852   Jeff Layton   vfs: embed struct...
198
  	final_putname(result);
3f9f0aa68   Linus Torvalds   VFS: clean up and...
199
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
  }
91a27b2a7   Jeff Layton   vfs: define struc...
201
202
  struct filename *
  getname(const char __user * filename)
f52e0c113   Al Viro   New AT_... flag: ...
203
  {
f7493e5d9   Linus Torvalds   vfs: tidy up spar...
204
  	return getname_flags(filename, 0, NULL);
f52e0c113   Al Viro   New AT_... flag: ...
205
  }
c4ad8f98b   Linus Torvalds   execve: use 'stru...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  /*
   * The "getname_kernel()" interface doesn't do pathnames longer
   * than EMBEDDED_NAME_MAX. Deal with it - you're a kernel user.
   */
  struct filename *
  getname_kernel(const char * filename)
  {
  	struct filename *result;
  	char *kname;
  	int len;
  
  	len = strlen(filename);
  	if (len >= EMBEDDED_NAME_MAX)
  		return ERR_PTR(-ENAMETOOLONG);
  
  	result = __getname();
  	if (unlikely(!result))
  		return ERR_PTR(-ENOMEM);
  
  	kname = (char *)result + sizeof(*result);
  	result->name = kname;
  	result->uptr = NULL;
  	result->aname = NULL;
  	result->separate = false;
  
  	strlcpy(kname, filename, EMBEDDED_NAME_MAX);
  	return result;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  #ifdef CONFIG_AUDITSYSCALL
91a27b2a7   Jeff Layton   vfs: define struc...
235
  void putname(struct filename *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
  {
5ac3a9c26   Al Viro   [PATCH] don't bot...
237
  	if (unlikely(!audit_dummy_context()))
91a27b2a7   Jeff Layton   vfs: define struc...
238
239
  		return audit_putname(name);
  	final_putname(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
  #endif
e77819e57   Linus Torvalds   vfs: move ACL cac...
242
243
  static int check_acl(struct inode *inode, int mask)
  {
84635d68b   Linus Torvalds   vfs: fix check_ac...
244
  #ifdef CONFIG_FS_POSIX_ACL
e77819e57   Linus Torvalds   vfs: move ACL cac...
245
  	struct posix_acl *acl;
e77819e57   Linus Torvalds   vfs: move ACL cac...
246
  	if (mask & MAY_NOT_BLOCK) {
3567866bf   Al Viro   RCUify freeing ac...
247
248
  		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  	        if (!acl)
e77819e57   Linus Torvalds   vfs: move ACL cac...
249
  	                return -EAGAIN;
3567866bf   Al Viro   RCUify freeing ac...
250
251
252
  		/* no ->get_acl() calls in RCU mode... */
  		if (acl == ACL_NOT_CACHED)
  			return -ECHILD;
206b1d09a   Ari Savolainen   Fix POSIX ACL per...
253
  	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
e77819e57   Linus Torvalds   vfs: move ACL cac...
254
  	}
2982baa2a   Christoph Hellwig   fs: add get_acl h...
255
256
257
  	acl = get_acl(inode, ACL_TYPE_ACCESS);
  	if (IS_ERR(acl))
  		return PTR_ERR(acl);
e77819e57   Linus Torvalds   vfs: move ACL cac...
258
259
260
261
262
  	if (acl) {
  	        int error = posix_acl_permission(inode, acl, mask);
  	        posix_acl_release(acl);
  	        return error;
  	}
84635d68b   Linus Torvalds   vfs: fix check_ac...
263
  #endif
e77819e57   Linus Torvalds   vfs: move ACL cac...
264
265
266
  
  	return -EAGAIN;
  }
5909ccaa3   Linus Torvalds   Make 'check_acl()...
267
  /*
948409c74   Andreas Gruenbacher   vfs: add a commen...
268
   * This does the basic permission checking
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
   */
7e40145eb   Al Viro   ->permission() sa...
270
  static int acl_permission_check(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
  {
26cf46be9   Linus Torvalds   vfs: micro-optimi...
272
  	unsigned int mode = inode->i_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273

8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
274
  	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275
276
  		mode >>= 6;
  	else {
e77819e57   Linus Torvalds   vfs: move ACL cac...
277
  		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
7e40145eb   Al Viro   ->permission() sa...
278
  			int error = check_acl(inode, mask);
b74c79e99   Nick Piggin   fs: provide rcu-w...
279
280
  			if (error != -EAGAIN)
  				return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
283
284
285
286
287
288
289
  		}
  
  		if (in_group_p(inode->i_gid))
  			mode >>= 3;
  	}
  
  	/*
  	 * If the DACs are ok we don't need any capability check.
  	 */
9c2c70392   Al Viro   ->permission() sa...
290
  	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
  		return 0;
5909ccaa3   Linus Torvalds   Make 'check_acl()...
292
293
294
295
  	return -EACCES;
  }
  
  /**
b74c79e99   Nick Piggin   fs: provide rcu-w...
296
   * generic_permission -  check for access rights on a Posix-like filesystem
5909ccaa3   Linus Torvalds   Make 'check_acl()...
297
   * @inode:	inode to check access rights for
8fd90c8d1   Andreas Gruenbacher   vfs: indicate tha...
298
   * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
5909ccaa3   Linus Torvalds   Make 'check_acl()...
299
300
301
302
   *
   * Used to check for read/write/execute permissions on a file.
   * We use "fsuid" for this, letting us set arbitrary permissions
   * for filesystem access without changing the "normal" uids which
b74c79e99   Nick Piggin   fs: provide rcu-w...
303
304
305
306
307
   * are used for other things.
   *
   * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
   * request cannot be satisfied (eg. requires blocking or too much complexity).
   * It would then be called again in ref-walk mode.
5909ccaa3   Linus Torvalds   Make 'check_acl()...
308
   */
2830ba7f3   Al Viro   ->permission() sa...
309
  int generic_permission(struct inode *inode, int mask)
5909ccaa3   Linus Torvalds   Make 'check_acl()...
310
311
312
313
  {
  	int ret;
  
  	/*
948409c74   Andreas Gruenbacher   vfs: add a commen...
314
  	 * Do the basic permission checks.
5909ccaa3   Linus Torvalds   Make 'check_acl()...
315
  	 */
7e40145eb   Al Viro   ->permission() sa...
316
  	ret = acl_permission_check(inode, mask);
5909ccaa3   Linus Torvalds   Make 'check_acl()...
317
318
  	if (ret != -EACCES)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319

d594e7ec4   Al Viro   massage generic_p...
320
321
  	if (S_ISDIR(inode->i_mode)) {
  		/* DACs are overridable for directories */
1a48e2ac0   Eric W. Biederman   userns: Replace t...
322
  		if (inode_capable(inode, CAP_DAC_OVERRIDE))
d594e7ec4   Al Viro   massage generic_p...
323
324
  			return 0;
  		if (!(mask & MAY_WRITE))
1a48e2ac0   Eric W. Biederman   userns: Replace t...
325
  			if (inode_capable(inode, CAP_DAC_READ_SEARCH))
d594e7ec4   Al Viro   massage generic_p...
326
327
328
  				return 0;
  		return -EACCES;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
329
330
  	/*
  	 * Read/write DACs are always overridable.
d594e7ec4   Al Viro   massage generic_p...
331
332
  	 * Executable DACs are overridable when there is
  	 * at least one exec bit set.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333
  	 */
d594e7ec4   Al Viro   massage generic_p...
334
  	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
1a48e2ac0   Eric W. Biederman   userns: Replace t...
335
  		if (inode_capable(inode, CAP_DAC_OVERRIDE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
340
  			return 0;
  
  	/*
  	 * Searching includes executable on directories, else just read.
  	 */
7ea660014   Serge E. Hallyn   generic_permissio...
341
  	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec4   Al Viro   massage generic_p...
342
  	if (mask == MAY_READ)
1a48e2ac0   Eric W. Biederman   userns: Replace t...
343
  		if (inode_capable(inode, CAP_DAC_READ_SEARCH))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
345
346
347
  			return 0;
  
  	return -EACCES;
  }
4d3595073   Al Viro   namei.c: move EXP...
348
  EXPORT_SYMBOL(generic_permission);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
349

3ddcd0569   Linus Torvalds   vfs: optimize ino...
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
  /*
   * We _really_ want to just do "generic_permission()" without
   * even looking at the inode->i_op values. So we keep a cache
   * flag in inode->i_opflags, that says "this has not special
   * permission function, use the fast case".
   */
  static inline int do_inode_permission(struct inode *inode, int mask)
  {
  	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  		if (likely(inode->i_op->permission))
  			return inode->i_op->permission(inode, mask);
  
  		/* This gets set once for the inode lifetime */
  		spin_lock(&inode->i_lock);
  		inode->i_opflags |= IOP_FASTPERM;
  		spin_unlock(&inode->i_lock);
  	}
  	return generic_permission(inode, mask);
  }
cb23beb55   Christoph Hellwig   kill vfs_permission
369
  /**
0bdaea901   David Howells   VFS: Split inode_...
370
371
372
   * __inode_permission - Check for access rights to a given inode
   * @inode: Inode to check permission on
   * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
cb23beb55   Christoph Hellwig   kill vfs_permission
373
   *
0bdaea901   David Howells   VFS: Split inode_...
374
   * Check for read/write/execute permissions on an inode.
948409c74   Andreas Gruenbacher   vfs: add a commen...
375
376
   *
   * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
0bdaea901   David Howells   VFS: Split inode_...
377
378
379
   *
   * This does not check for a read-only file system.  You probably want
   * inode_permission().
cb23beb55   Christoph Hellwig   kill vfs_permission
380
   */
0bdaea901   David Howells   VFS: Split inode_...
381
  int __inode_permission(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382
  {
e6305c43e   Al Viro   [PATCH] sanitize ...
383
  	int retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384

3ddcd0569   Linus Torvalds   vfs: optimize ino...
385
  	if (unlikely(mask & MAY_WRITE)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
388
389
390
391
  		/*
  		 * Nobody gets write access to an immutable file.
  		 */
  		if (IS_IMMUTABLE(inode))
  			return -EACCES;
  	}
3ddcd0569   Linus Torvalds   vfs: optimize ino...
392
  	retval = do_inode_permission(inode, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
394
  	if (retval)
  		return retval;
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
395
396
397
  	retval = devcgroup_inode_permission(inode, mask);
  	if (retval)
  		return retval;
d09ca7397   Eric Paris   security: make LS...
398
  	return security_inode_permission(inode, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
  }
f4d6ff89d   Al Viro   move exec_permiss...
400
  /**
0bdaea901   David Howells   VFS: Split inode_...
401
402
   * sb_permission - Check superblock-level permissions
   * @sb: Superblock of inode to check permission on
55852635a   Randy Dunlap   fs: fix fs/namei....
403
   * @inode: Inode to check permission on
0bdaea901   David Howells   VFS: Split inode_...
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
   * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
   *
   * Separate out file-system wide checks from inode-specific permission checks.
   */
  static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
  {
  	if (unlikely(mask & MAY_WRITE)) {
  		umode_t mode = inode->i_mode;
  
  		/* Nobody gets write access to a read-only fs. */
  		if ((sb->s_flags & MS_RDONLY) &&
  		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  			return -EROFS;
  	}
  	return 0;
  }
  
  /**
   * inode_permission - Check for access rights to a given inode
   * @inode: Inode to check permission on
   * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
   *
   * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
   * this, letting us set arbitrary permissions for filesystem access without
   * changing the "normal" UIDs which are used for other things.
   *
   * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
   */
  int inode_permission(struct inode *inode, int mask)
  {
  	int retval;
  
  	retval = sb_permission(inode->i_sb, inode, mask);
  	if (retval)
  		return retval;
  	return __inode_permission(inode, mask);
  }
4d3595073   Al Viro   namei.c: move EXP...
441
  EXPORT_SYMBOL(inode_permission);
0bdaea901   David Howells   VFS: Split inode_...
442
443
  
  /**
5dd784d04   Jan Blunck   Introduce path_get()
444
445
446
447
448
   * path_get - get a reference to a path
   * @path: path to get the reference to
   *
   * Given a path increment the reference count to the dentry and the vfsmount.
   */
dcf787f39   Al Viro   constify path_get...
449
  void path_get(const struct path *path)
5dd784d04   Jan Blunck   Introduce path_get()
450
451
452
453
454
455
456
  {
  	mntget(path->mnt);
  	dget(path->dentry);
  }
  EXPORT_SYMBOL(path_get);
  
  /**
1d957f9bf   Jan Blunck   Introduce path_put()
457
458
459
460
461
   * path_put - put a reference to a path
   * @path: path to put the reference to
   *
   * Given a path decrement the reference count to the dentry and the vfsmount.
   */
dcf787f39   Al Viro   constify path_get...
462
  void path_put(const struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
  {
1d957f9bf   Jan Blunck   Introduce path_put()
464
465
  	dput(path->dentry);
  	mntput(path->mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  }
1d957f9bf   Jan Blunck   Introduce path_put()
467
  EXPORT_SYMBOL(path_put);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468

19660af73   Al Viro   consolidate namei...
469
  /*
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
470
   * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af73   Al Viro   consolidate namei...
471
472
473
474
475
476
477
   * Documentation/filesystems/path-lookup.txt).  In situations when we can't
   * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
   * normal reference counts on dentries and vfsmounts to transition to rcu-walk
   * mode.  Refcounts are grabbed at the last known good point before rcu-walk
   * got stuck, so ref-walk may continue from there. If this is not successful
   * (eg. a seqcount has changed), then failure is returned and it's up to caller
   * to restart the path walk from the beginning in ref-walk mode.
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
478
   */
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
479
480
  
  /**
19660af73   Al Viro   consolidate namei...
481
482
483
   * unlazy_walk - try to switch to ref-walk mode.
   * @nd: nameidata pathwalk data
   * @dentry: child of nd->path.dentry or NULL
39191628e   Randy Dunlap   fs: fix namei.c k...
484
   * Returns: 0 on success, -ECHILD on failure
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
485
   *
19660af73   Al Viro   consolidate namei...
486
487
488
   * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
   * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
   * @nd or NULL.  Must be called from rcu-walk context.
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
489
   */
19660af73   Al Viro   consolidate namei...
490
  static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
491
492
493
494
495
  {
  	struct fs_struct *fs = current->fs;
  	struct dentry *parent = nd->path.dentry;
  
  	BUG_ON(!(nd->flags & LOOKUP_RCU));
e5c832d55   Linus Torvalds   vfs: fix dentry R...
496
497
  
  	/*
48a066e72   Al Viro   RCU'd vfsmounts
498
499
500
501
502
503
  	 * After legitimizing the bastards, terminate_walk()
  	 * will do the right thing for non-RCU mode, and all our
  	 * subsequent exit cases should rcu_read_unlock()
  	 * before returning.  Do vfsmount first; if dentry
  	 * can't be legitimized, just set nd->path.dentry to NULL
  	 * and rely on dput(NULL) being a no-op.
e5c832d55   Linus Torvalds   vfs: fix dentry R...
504
  	 */
48a066e72   Al Viro   RCU'd vfsmounts
505
  	if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
e5c832d55   Linus Torvalds   vfs: fix dentry R...
506
  		return -ECHILD;
e5c832d55   Linus Torvalds   vfs: fix dentry R...
507
  	nd->flags &= ~LOOKUP_RCU;
15570086b   Linus Torvalds   vfs: reimplement ...
508

48a066e72   Al Viro   RCU'd vfsmounts
509
510
  	if (!lockref_get_not_dead(&parent->d_lockref)) {
  		nd->path.dentry = NULL;	
d870b4a19   Al Viro   fix bogus path_pu...
511
  		goto out;
48a066e72   Al Viro   RCU'd vfsmounts
512
  	}
15570086b   Linus Torvalds   vfs: reimplement ...
513
514
515
516
517
518
519
520
521
522
523
  	/*
  	 * For a negative lookup, the lookup sequence point is the parents
  	 * sequence point, and it only needs to revalidate the parent dentry.
  	 *
  	 * For a positive lookup, we need to move both the parent and the
  	 * dentry from the RCU domain to be properly refcounted. And the
  	 * sequence number in the dentry validates *both* dentry counters,
  	 * since we checked the sequence number of the parent after we got
  	 * the child sequence number. So we know the parent must still
  	 * be valid if the child sequence number is still valid.
  	 */
19660af73   Al Viro   consolidate namei...
524
  	if (!dentry) {
e5c832d55   Linus Torvalds   vfs: fix dentry R...
525
526
  		if (read_seqcount_retry(&parent->d_seq, nd->seq))
  			goto out;
19660af73   Al Viro   consolidate namei...
527
528
  		BUG_ON(nd->inode != parent->d_inode);
  	} else {
e5c832d55   Linus Torvalds   vfs: fix dentry R...
529
530
531
532
  		if (!lockref_get_not_dead(&dentry->d_lockref))
  			goto out;
  		if (read_seqcount_retry(&dentry->d_seq, nd->seq))
  			goto drop_dentry;
19660af73   Al Viro   consolidate namei...
533
  	}
e5c832d55   Linus Torvalds   vfs: fix dentry R...
534
535
536
537
538
539
540
541
542
  
  	/*
  	 * Sequence counts matched. Now make sure that the root is
  	 * still valid and get it if required.
  	 */
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  		spin_lock(&fs->lock);
  		if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
  			goto unlock_and_drop_dentry;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
543
544
545
  		path_get(&nd->root);
  		spin_unlock(&fs->lock);
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
546

8b61e74ff   Al Viro   get rid of {lock,...
547
  	rcu_read_unlock();
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
548
  	return 0;
19660af73   Al Viro   consolidate namei...
549

e5c832d55   Linus Torvalds   vfs: fix dentry R...
550
551
552
  unlock_and_drop_dentry:
  	spin_unlock(&fs->lock);
  drop_dentry:
8b61e74ff   Al Viro   get rid of {lock,...
553
  	rcu_read_unlock();
15570086b   Linus Torvalds   vfs: reimplement ...
554
  	dput(dentry);
d0d272771   Linus Torvalds   vfs: make sure we...
555
  	goto drop_root_mnt;
e5c832d55   Linus Torvalds   vfs: fix dentry R...
556
  out:
8b61e74ff   Al Viro   get rid of {lock,...
557
  	rcu_read_unlock();
d0d272771   Linus Torvalds   vfs: make sure we...
558
559
560
  drop_root_mnt:
  	if (!(nd->flags & LOOKUP_ROOT))
  		nd->root.mnt = NULL;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
561
562
  	return -ECHILD;
  }
4ce16ef3f   Al Viro   fs/namei.c: don't...
563
  static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
34286d666   Nick Piggin   fs: rcu-walk awar...
564
  {
4ce16ef3f   Al Viro   fs/namei.c: don't...
565
  	return dentry->d_op->d_revalidate(dentry, flags);
34286d666   Nick Piggin   fs: rcu-walk awar...
566
  }
9f1fafee9   Al Viro   merge handle_reva...
567
568
569
  /**
   * complete_walk - successful completion of path walk
   * @nd:  pointer nameidata
39159de2a   Jeff Layton   vfs: force reval ...
570
   *
9f1fafee9   Al Viro   merge handle_reva...
571
572
573
574
575
   * If we had been in RCU mode, drop out of it and legitimize nd->path.
   * Revalidate the final result, unless we'd already done that during
   * the path walk or the filesystem doesn't ask for it.  Return 0 on
   * success, -error on failure.  In case of failure caller does not
   * need to drop nd->path.
39159de2a   Jeff Layton   vfs: force reval ...
576
   */
9f1fafee9   Al Viro   merge handle_reva...
577
  static int complete_walk(struct nameidata *nd)
39159de2a   Jeff Layton   vfs: force reval ...
578
  {
16c2cd717   Al Viro   untangle the "nee...
579
  	struct dentry *dentry = nd->path.dentry;
39159de2a   Jeff Layton   vfs: force reval ...
580
  	int status;
39159de2a   Jeff Layton   vfs: force reval ...
581

9f1fafee9   Al Viro   merge handle_reva...
582
583
584
585
  	if (nd->flags & LOOKUP_RCU) {
  		nd->flags &= ~LOOKUP_RCU;
  		if (!(nd->flags & LOOKUP_ROOT))
  			nd->root.mnt = NULL;
15570086b   Linus Torvalds   vfs: reimplement ...
586

48a066e72   Al Viro   RCU'd vfsmounts
587
  		if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
8b61e74ff   Al Viro   get rid of {lock,...
588
  			rcu_read_unlock();
48a066e72   Al Viro   RCU'd vfsmounts
589
590
  			return -ECHILD;
  		}
e5c832d55   Linus Torvalds   vfs: fix dentry R...
591
  		if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
8b61e74ff   Al Viro   get rid of {lock,...
592
  			rcu_read_unlock();
48a066e72   Al Viro   RCU'd vfsmounts
593
  			mntput(nd->path.mnt);
e5c832d55   Linus Torvalds   vfs: fix dentry R...
594
595
596
  			return -ECHILD;
  		}
  		if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
8b61e74ff   Al Viro   get rid of {lock,...
597
  			rcu_read_unlock();
e5c832d55   Linus Torvalds   vfs: fix dentry R...
598
  			dput(dentry);
48a066e72   Al Viro   RCU'd vfsmounts
599
  			mntput(nd->path.mnt);
9f1fafee9   Al Viro   merge handle_reva...
600
601
  			return -ECHILD;
  		}
8b61e74ff   Al Viro   get rid of {lock,...
602
  		rcu_read_unlock();
9f1fafee9   Al Viro   merge handle_reva...
603
  	}
16c2cd717   Al Viro   untangle the "nee...
604
605
  	if (likely(!(nd->flags & LOOKUP_JUMPED)))
  		return 0;
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
606
  	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
39159de2a   Jeff Layton   vfs: force reval ...
607
  		return 0;
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
608
  	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
39159de2a   Jeff Layton   vfs: force reval ...
609
610
  	if (status > 0)
  		return 0;
16c2cd717   Al Viro   untangle the "nee...
611
  	if (!status)
39159de2a   Jeff Layton   vfs: force reval ...
612
  		status = -ESTALE;
16c2cd717   Al Viro   untangle the "nee...
613

9f1fafee9   Al Viro   merge handle_reva...
614
  	path_put(&nd->path);
39159de2a   Jeff Layton   vfs: force reval ...
615
616
  	return status;
  }
2a7378711   Al Viro   Cache root in nam...
617
618
  static __always_inline void set_root(struct nameidata *nd)
  {
f7ad3c6be   Miklos Szeredi   vfs: add helpers ...
619
620
  	if (!nd->root.mnt)
  		get_fs_root(current->fs, &nd->root);
2a7378711   Al Viro   Cache root in nam...
621
  }
6de88d729   Al Viro   kill __link_path_...
622
  static int link_path_walk(const char *, struct nameidata *);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
623
624
625
626
  static __always_inline void set_root_rcu(struct nameidata *nd)
  {
  	if (!nd->root.mnt) {
  		struct fs_struct *fs = current->fs;
c28cc3646   Nick Piggin   fs: fs_struct use...
627
628
629
630
631
  		unsigned seq;
  
  		do {
  			seq = read_seqcount_begin(&fs->seq);
  			nd->root = fs->root;
c1530019e   Tim Chen   vfs: Fix absolute...
632
  			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
c28cc3646   Nick Piggin   fs: fs_struct use...
633
  		} while (read_seqcount_retry(&fs->seq, seq));
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
634
635
  	}
  }
1d957f9bf   Jan Blunck   Introduce path_put()
636
  static void path_put_conditional(struct path *path, struct nameidata *nd)
051d38125   Ian Kent   [PATCH] autofs4: ...
637
638
  {
  	dput(path->dentry);
4ac913785   Jan Blunck   Embed a struct pa...
639
  	if (path->mnt != nd->path.mnt)
051d38125   Ian Kent   [PATCH] autofs4: ...
640
641
  		mntput(path->mnt);
  }
7b9337aaf   Nick Piggin   fs: namei fix ->p...
642
643
  static inline void path_to_nameidata(const struct path *path,
  					struct nameidata *nd)
051d38125   Ian Kent   [PATCH] autofs4: ...
644
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
645
646
647
648
  	if (!(nd->flags & LOOKUP_RCU)) {
  		dput(nd->path.dentry);
  		if (nd->path.mnt != path->mnt)
  			mntput(nd->path.mnt);
9a2296832   Huang Shijie   namei.c : update ...
649
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
650
  	nd->path.mnt = path->mnt;
4ac913785   Jan Blunck   Embed a struct pa...
651
  	nd->path.dentry = path->dentry;
051d38125   Ian Kent   [PATCH] autofs4: ...
652
  }
b5fb63c18   Christoph Hellwig   fs: add nd_jump_link
653
654
655
656
657
658
659
660
661
662
663
  /*
   * Helper to directly jump to a known parsed path from ->follow_link,
   * caller must have taken a reference to path beforehand.
   */
  void nd_jump_link(struct nameidata *nd, struct path *path)
  {
  	path_put(&nd->path);
  
  	nd->path = *path;
  	nd->inode = nd->path.dentry->d_inode;
  	nd->flags |= LOOKUP_JUMPED;
b5fb63c18   Christoph Hellwig   fs: add nd_jump_link
664
  }
574197e0d   Al Viro   tidy the trailing...
665
666
667
  static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  {
  	struct inode *inode = link->dentry->d_inode;
6d7b5aaed   Al Viro   namei.c: let foll...
668
  	if (inode->i_op->put_link)
574197e0d   Al Viro   tidy the trailing...
669
670
671
  		inode->i_op->put_link(link->dentry, nd, cookie);
  	path_put(link);
  }
561ec64ae   Linus Torvalds   VFS: don't do pro...
672
673
  int sysctl_protected_symlinks __read_mostly = 0;
  int sysctl_protected_hardlinks __read_mostly = 0;
800179c9b   Kees Cook   fs: add link rest...
674
675
676
677
  
  /**
   * may_follow_link - Check symlink following for unsafe situations
   * @link: The path of the symlink
55852635a   Randy Dunlap   fs: fix fs/namei....
678
   * @nd: nameidata pathwalk data
800179c9b   Kees Cook   fs: add link rest...
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
   *
   * In the case of the sysctl_protected_symlinks sysctl being enabled,
   * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
   * in a sticky world-writable directory. This is to protect privileged
   * processes from failing races against path names that may change out
   * from under them by way of other users creating malicious symlinks.
   * It will permit symlinks to be followed only when outside a sticky
   * world-writable directory, or when the uid of the symlink and follower
   * match, or when the directory owner matches the symlink's owner.
   *
   * Returns 0 if following the symlink is allowed, -ve on error.
   */
  static inline int may_follow_link(struct path *link, struct nameidata *nd)
  {
  	const struct inode *inode;
  	const struct inode *parent;
  
  	if (!sysctl_protected_symlinks)
  		return 0;
  
  	/* Allowed if owner and follower match. */
  	inode = link->dentry->d_inode;
81abe27b1   Eric W. Biederman   userns: Fix link...
701
  	if (uid_eq(current_cred()->fsuid, inode->i_uid))
800179c9b   Kees Cook   fs: add link rest...
702
703
704
705
706
707
708
709
  		return 0;
  
  	/* Allowed if parent directory not sticky and world-writable. */
  	parent = nd->path.dentry->d_inode;
  	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
  		return 0;
  
  	/* Allowed if parent directory and link owner match. */
81abe27b1   Eric W. Biederman   userns: Fix link...
710
  	if (uid_eq(parent->i_uid, inode->i_uid))
800179c9b   Kees Cook   fs: add link rest...
711
  		return 0;
ffd8d101a   Sasha Levin   fs: prevent use a...
712
  	audit_log_link_denied("follow_link", link);
800179c9b   Kees Cook   fs: add link rest...
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  	path_put_conditional(link, nd);
  	path_put(&nd->path);
  	return -EACCES;
  }
  
  /**
   * safe_hardlink_source - Check for safe hardlink conditions
   * @inode: the source inode to hardlink from
   *
   * Return false if at least one of the following conditions:
   *    - inode is not a regular file
   *    - inode is setuid
   *    - inode is setgid and group-exec
   *    - access failure for read and write
   *
   * Otherwise returns true.
   */
  static bool safe_hardlink_source(struct inode *inode)
  {
  	umode_t mode = inode->i_mode;
  
  	/* Special files should not get pinned to the filesystem. */
  	if (!S_ISREG(mode))
  		return false;
  
  	/* Setuid files should not get pinned to the filesystem. */
  	if (mode & S_ISUID)
  		return false;
  
  	/* Executable setgid files should not get pinned to the filesystem. */
  	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  		return false;
  
  	/* Hardlinking to unreadable or unwritable sources is dangerous. */
  	if (inode_permission(inode, MAY_READ | MAY_WRITE))
  		return false;
  
  	return true;
  }
  
  /**
   * may_linkat - Check permissions for creating a hardlink
   * @link: the source to hardlink from
   *
   * Block hardlink when all of:
   *  - sysctl_protected_hardlinks enabled
   *  - fsuid does not match inode
   *  - hardlink source is unsafe (see safe_hardlink_source() above)
   *  - not CAP_FOWNER
   *
   * Returns 0 if successful, -ve on error.
   */
  static int may_linkat(struct path *link)
  {
  	const struct cred *cred;
  	struct inode *inode;
  
  	if (!sysctl_protected_hardlinks)
  		return 0;
  
  	cred = current_cred();
  	inode = link->dentry->d_inode;
  
  	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
  	 * otherwise, it must be a safe source.
  	 */
81abe27b1   Eric W. Biederman   userns: Fix link...
779
  	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
800179c9b   Kees Cook   fs: add link rest...
780
781
  	    capable(CAP_FOWNER))
  		return 0;
a51d9eaa4   Kees Cook   fs: add link rest...
782
  	audit_log_link_denied("linkat", link);
800179c9b   Kees Cook   fs: add link rest...
783
784
  	return -EPERM;
  }
def4af30c   Al Viro   Get rid of symlin...
785
  static __always_inline int
574197e0d   Al Viro   tidy the trailing...
786
  follow_link(struct path *link, struct nameidata *nd, void **p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
  {
7b9337aaf   Nick Piggin   fs: namei fix ->p...
788
  	struct dentry *dentry = link->dentry;
6d7b5aaed   Al Viro   namei.c: let foll...
789
790
  	int error;
  	char *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791

844a39179   Al Viro   nothing in do_fol...
792
  	BUG_ON(nd->flags & LOOKUP_RCU);
0e794589e   Al Viro   fix follow_link()...
793
794
  	if (link->mnt == nd->path.mnt)
  		mntget(link->mnt);
6d7b5aaed   Al Viro   namei.c: let foll...
795
796
797
  	error = -ELOOP;
  	if (unlikely(current->total_link_count >= 40))
  		goto out_put_nd_path;
574197e0d   Al Viro   tidy the trailing...
798
799
  	cond_resched();
  	current->total_link_count++;
68ac1234f   Al Viro   switch touch_atim...
800
  	touch_atime(link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
  	nd_set_link(nd, NULL);
cd4e91d3b   Al Viro   [PATCH] namei fix...
802

36f3b4f69   Al Viro   pull security_ino...
803
  	error = security_inode_follow_link(link->dentry, nd);
6d7b5aaed   Al Viro   namei.c: let foll...
804
805
  	if (error)
  		goto out_put_nd_path;
36f3b4f69   Al Viro   pull security_ino...
806

86acdca1b   Al Viro   fix autofs/afs/et...
807
  	nd->last_type = LAST_BIND;
def4af30c   Al Viro   Get rid of symlin...
808
809
  	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
  	error = PTR_ERR(*p);
6d7b5aaed   Al Viro   namei.c: let foll...
810
  	if (IS_ERR(*p))
408ef013c   Christoph Hellwig   fs: move path_put...
811
  		goto out_put_nd_path;
6d7b5aaed   Al Viro   namei.c: let foll...
812
813
814
815
  
  	error = 0;
  	s = nd_get_link(nd);
  	if (s) {
443ed254c   Al Viro   ... and fold the ...
816
817
818
819
820
821
822
823
824
825
826
827
828
829
  		if (unlikely(IS_ERR(s))) {
  			path_put(&nd->path);
  			put_link(nd, link, *p);
  			return PTR_ERR(s);
  		}
  		if (*s == '/') {
  			set_root(nd);
  			path_put(&nd->path);
  			nd->path = nd->root;
  			path_get(&nd->root);
  			nd->flags |= LOOKUP_JUMPED;
  		}
  		nd->inode = nd->path.dentry->d_inode;
  		error = link_path_walk(s, nd);
b5fb63c18   Christoph Hellwig   fs: add nd_jump_link
830
831
  		if (unlikely(error))
  			put_link(nd, link, *p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
832
  	}
6d7b5aaed   Al Viro   namei.c: let foll...
833
834
835
836
  
  	return error;
  
  out_put_nd_path:
98f6ef64b   Arnd Bergmann   vfs: bogus warnin...
837
  	*p = NULL;
6d7b5aaed   Al Viro   namei.c: let foll...
838
  	path_put(&nd->path);
6d7b5aaed   Al Viro   namei.c: let foll...
839
  	path_put(link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
840
841
  	return error;
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
842
843
  static int follow_up_rcu(struct path *path)
  {
0714a5338   Al Viro   vfs: now it can b...
844
845
  	struct mount *mnt = real_mount(path->mnt);
  	struct mount *parent;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
846
  	struct dentry *mountpoint;
0714a5338   Al Viro   vfs: now it can b...
847
848
  	parent = mnt->mnt_parent;
  	if (&parent->mnt == path->mnt)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
849
  		return 0;
a73324da7   Al Viro   vfs: move mnt_mou...
850
  	mountpoint = mnt->mnt_mountpoint;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
851
  	path->dentry = mountpoint;
0714a5338   Al Viro   vfs: now it can b...
852
  	path->mnt = &parent->mnt;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
853
854
  	return 1;
  }
f015f1267   David Howells   VFS: Comment moun...
855
856
857
858
859
860
861
862
863
864
  /*
   * follow_up - Find the mountpoint of path's vfsmount
   *
   * Given a path, find the mountpoint of its source file system.
   * Replace @path with the path of the mountpoint in the parent mount.
   * Up is towards /.
   *
   * Return 1 if we went up a level and 0 if we were already at the
   * root.
   */
bab77ebf5   Al Viro   switch follow_up(...
865
  int follow_up(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
866
  {
0714a5338   Al Viro   vfs: now it can b...
867
868
  	struct mount *mnt = real_mount(path->mnt);
  	struct mount *parent;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
  	struct dentry *mountpoint;
99b7db7b8   Nick Piggin   fs: brlock vfsmou...
870

48a066e72   Al Viro   RCU'd vfsmounts
871
  	read_seqlock_excl(&mount_lock);
0714a5338   Al Viro   vfs: now it can b...
872
  	parent = mnt->mnt_parent;
3c0a61636   Al Viro   unobfuscate follo...
873
  	if (parent == mnt) {
48a066e72   Al Viro   RCU'd vfsmounts
874
  		read_sequnlock_excl(&mount_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
876
  		return 0;
  	}
0714a5338   Al Viro   vfs: now it can b...
877
  	mntget(&parent->mnt);
a73324da7   Al Viro   vfs: move mnt_mou...
878
  	mountpoint = dget(mnt->mnt_mountpoint);
48a066e72   Al Viro   RCU'd vfsmounts
879
  	read_sequnlock_excl(&mount_lock);
bab77ebf5   Al Viro   switch follow_up(...
880
881
882
  	dput(path->dentry);
  	path->dentry = mountpoint;
  	mntput(path->mnt);
0714a5338   Al Viro   vfs: now it can b...
883
  	path->mnt = &parent->mnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
884
885
  	return 1;
  }
4d3595073   Al Viro   namei.c: move EXP...
886
  EXPORT_SYMBOL(follow_up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887

b5c84bf6f   Nick Piggin   fs: dcache remove...
888
  /*
9875cf806   David Howells   Add a dentry op t...
889
890
891
   * Perform an automount
   * - return -EISDIR to tell follow_managed() to stop and return the path we
   *   were called with.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
892
   */
9875cf806   David Howells   Add a dentry op t...
893
894
  static int follow_automount(struct path *path, unsigned flags,
  			    bool *need_mntput)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
895
  {
9875cf806   David Howells   Add a dentry op t...
896
  	struct vfsmount *mnt;
ea5b778a8   David Howells   Unexport do_add_m...
897
  	int err;
9875cf806   David Howells   Add a dentry op t...
898
899
900
  
  	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  		return -EREMOTE;
0ec26fd06   Miklos Szeredi   vfs: automount sh...
901
902
903
904
905
906
907
908
909
910
  	/* We don't want to mount if someone's just doing a stat -
  	 * unless they're stat'ing a directory and appended a '/' to
  	 * the name.
  	 *
  	 * We do, however, want to mount if someone wants to open or
  	 * create a file of any type under the mountpoint, wants to
  	 * traverse through the mountpoint or wants to open the
  	 * mounted directory.  Also, autofs may mark negative dentries
  	 * as being automount points.  These will need the attentions
  	 * of the daemon to instantiate them before they can be used.
9875cf806   David Howells   Add a dentry op t...
911
  	 */
0ec26fd06   Miklos Szeredi   vfs: automount sh...
912
  	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
d94c177be   Linus Torvalds   vfs pathname look...
913
  		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
0ec26fd06   Miklos Szeredi   vfs: automount sh...
914
915
  	    path->dentry->d_inode)
  		return -EISDIR;
9875cf806   David Howells   Add a dentry op t...
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
  	current->total_link_count++;
  	if (current->total_link_count >= 40)
  		return -ELOOP;
  
  	mnt = path->dentry->d_op->d_automount(path);
  	if (IS_ERR(mnt)) {
  		/*
  		 * The filesystem is allowed to return -EISDIR here to indicate
  		 * it doesn't want to automount.  For instance, autofs would do
  		 * this so that its userspace daemon can mount on this dentry.
  		 *
  		 * However, we can only permit this if it's a terminal point in
  		 * the path being looked up; if it wasn't then the remainder of
  		 * the path is inaccessible and we should say so.
  		 */
49084c3bb   Al Viro   kill LOOKUP_CONTINUE
931
  		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9875cf806   David Howells   Add a dentry op t...
932
933
  			return -EREMOTE;
  		return PTR_ERR(mnt);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
934
  	}
ea5b778a8   David Howells   Unexport do_add_m...
935

9875cf806   David Howells   Add a dentry op t...
936
937
  	if (!mnt) /* mount collision */
  		return 0;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
938

8aef18845   Al Viro   VFS: Fix vfsmount...
939
940
941
942
943
  	if (!*need_mntput) {
  		/* lock_mount() may release path->mnt on error */
  		mntget(path->mnt);
  		*need_mntput = true;
  	}
19a167af7   Al Viro   Take the completi...
944
  	err = finish_automount(mnt, path);
9875cf806   David Howells   Add a dentry op t...
945

ea5b778a8   David Howells   Unexport do_add_m...
946
947
948
  	switch (err) {
  	case -EBUSY:
  		/* Someone else made a mount here whilst we were busy */
19a167af7   Al Viro   Take the completi...
949
  		return 0;
ea5b778a8   David Howells   Unexport do_add_m...
950
  	case 0:
8aef18845   Al Viro   VFS: Fix vfsmount...
951
  		path_put(path);
ea5b778a8   David Howells   Unexport do_add_m...
952
953
  		path->mnt = mnt;
  		path->dentry = dget(mnt->mnt_root);
ea5b778a8   David Howells   Unexport do_add_m...
954
  		return 0;
19a167af7   Al Viro   Take the completi...
955
956
  	default:
  		return err;
ea5b778a8   David Howells   Unexport do_add_m...
957
  	}
19a167af7   Al Viro   Take the completi...
958

463ffb2e9   Al Viro   [PATCH] namei fix...
959
  }
9875cf806   David Howells   Add a dentry op t...
960
961
  /*
   * Handle a dentry that is managed in some way.
cc53ce53c   David Howells   Add a dentry op t...
962
   * - Flagged for transit management (autofs)
9875cf806   David Howells   Add a dentry op t...
963
964
965
966
967
968
969
970
   * - Flagged as mountpoint
   * - Flagged as automount point
   *
   * This may only be called in refwalk mode.
   *
   * Serialization is taken care of in namespace.c
   */
  static int follow_managed(struct path *path, unsigned flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
  {
8aef18845   Al Viro   VFS: Fix vfsmount...
972
  	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9875cf806   David Howells   Add a dentry op t...
973
974
  	unsigned managed;
  	bool need_mntput = false;
8aef18845   Al Viro   VFS: Fix vfsmount...
975
  	int ret = 0;
9875cf806   David Howells   Add a dentry op t...
976
977
978
979
980
981
982
  
  	/* Given that we're not holding a lock here, we retain the value in a
  	 * local variable for each dentry as we look at it so that we don't see
  	 * the components of that value change under us */
  	while (managed = ACCESS_ONCE(path->dentry->d_flags),
  	       managed &= DCACHE_MANAGED_DENTRY,
  	       unlikely(managed != 0)) {
cc53ce53c   David Howells   Add a dentry op t...
983
984
985
986
987
  		/* Allow the filesystem to manage the transit without i_mutex
  		 * being held. */
  		if (managed & DCACHE_MANAGE_TRANSIT) {
  			BUG_ON(!path->dentry->d_op);
  			BUG_ON(!path->dentry->d_op->d_manage);
1aed3e420   Al Viro   lose 'mounting_he...
988
  			ret = path->dentry->d_op->d_manage(path->dentry, false);
cc53ce53c   David Howells   Add a dentry op t...
989
  			if (ret < 0)
8aef18845   Al Viro   VFS: Fix vfsmount...
990
  				break;
cc53ce53c   David Howells   Add a dentry op t...
991
  		}
9875cf806   David Howells   Add a dentry op t...
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  		/* Transit to a mounted filesystem. */
  		if (managed & DCACHE_MOUNTED) {
  			struct vfsmount *mounted = lookup_mnt(path);
  			if (mounted) {
  				dput(path->dentry);
  				if (need_mntput)
  					mntput(path->mnt);
  				path->mnt = mounted;
  				path->dentry = dget(mounted->mnt_root);
  				need_mntput = true;
  				continue;
  			}
  
  			/* Something is mounted on this dentry in another
  			 * namespace and/or whatever was mounted there in this
48a066e72   Al Viro   RCU'd vfsmounts
1007
1008
  			 * namespace got unmounted before lookup_mnt() could
  			 * get it */
9875cf806   David Howells   Add a dentry op t...
1009
1010
1011
1012
1013
1014
  		}
  
  		/* Handle an automount point */
  		if (managed & DCACHE_NEED_AUTOMOUNT) {
  			ret = follow_automount(path, flags, &need_mntput);
  			if (ret < 0)
8aef18845   Al Viro   VFS: Fix vfsmount...
1015
  				break;
9875cf806   David Howells   Add a dentry op t...
1016
1017
1018
1019
1020
  			continue;
  		}
  
  		/* We didn't change the current path point */
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1021
  	}
8aef18845   Al Viro   VFS: Fix vfsmount...
1022
1023
1024
1025
1026
  
  	if (need_mntput && path->mnt == mnt)
  		mntput(path->mnt);
  	if (ret == -EISDIR)
  		ret = 0;
a3fbbde70   Al Viro   VFS: we need to s...
1027
  	return ret < 0 ? ret : need_mntput;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1028
  }
cc53ce53c   David Howells   Add a dentry op t...
1029
  int follow_down_one(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1030
1031
  {
  	struct vfsmount *mounted;
1c755af4d   Al Viro   switch lookup_mnt()
1032
  	mounted = lookup_mnt(path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
  	if (mounted) {
9393bd07c   Al Viro   switch follow_down()
1034
1035
1036
1037
  		dput(path->dentry);
  		mntput(path->mnt);
  		path->mnt = mounted;
  		path->dentry = dget(mounted->mnt_root);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1038
1039
1040
1041
  		return 1;
  	}
  	return 0;
  }
4d3595073   Al Viro   namei.c: move EXP...
1042
  EXPORT_SYMBOL(follow_down_one);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1043

62a7375e5   Ian Kent   vfs - check non-m...
1044
1045
1046
1047
1048
  static inline bool managed_dentry_might_block(struct dentry *dentry)
  {
  	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
  		dentry->d_op->d_manage(dentry, true) < 0);
  }
9875cf806   David Howells   Add a dentry op t...
1049
  /*
287548e46   Al Viro   split __follow_mo...
1050
1051
   * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
   * we meet a managed dentry that would need blocking.
9875cf806   David Howells   Add a dentry op t...
1052
1053
   */
  static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
287548e46   Al Viro   split __follow_mo...
1054
  			       struct inode **inode)
9875cf806   David Howells   Add a dentry op t...
1055
  {
62a7375e5   Ian Kent   vfs - check non-m...
1056
  	for (;;) {
c71053659   Al Viro   vfs: spread struc...
1057
  		struct mount *mounted;
62a7375e5   Ian Kent   vfs - check non-m...
1058
1059
1060
1061
  		/*
  		 * Don't forget we might have a non-mountpoint managed dentry
  		 * that wants to block transit.
  		 */
287548e46   Al Viro   split __follow_mo...
1062
  		if (unlikely(managed_dentry_might_block(path->dentry)))
ab90911ff   David Howells   Allow d_manage() ...
1063
  			return false;
62a7375e5   Ian Kent   vfs - check non-m...
1064
1065
  
  		if (!d_mountpoint(path->dentry))
b37199e62   Al Viro   rcuwalk: recheck ...
1066
  			return true;
62a7375e5   Ian Kent   vfs - check non-m...
1067

474279dc0   Al Viro   split __lookup_mn...
1068
  		mounted = __lookup_mnt(path->mnt, path->dentry);
9875cf806   David Howells   Add a dentry op t...
1069
1070
  		if (!mounted)
  			break;
c71053659   Al Viro   vfs: spread struc...
1071
1072
  		path->mnt = &mounted->mnt;
  		path->dentry = mounted->mnt.mnt_root;
a3fbbde70   Al Viro   VFS: we need to s...
1073
  		nd->flags |= LOOKUP_JUMPED;
9875cf806   David Howells   Add a dentry op t...
1074
  		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
594302624   Linus Torvalds   vfs: fix race in ...
1075
1076
1077
1078
1079
1080
  		/*
  		 * Update the inode too. We don't need to re-check the
  		 * dentry sequence number here after this d_inode read,
  		 * because a mount-point is always pinned.
  		 */
  		*inode = path->dentry->d_inode;
9875cf806   David Howells   Add a dentry op t...
1081
  	}
b37199e62   Al Viro   rcuwalk: recheck ...
1082
  	return read_seqretry(&mount_lock, nd->m_seq);
287548e46   Al Viro   split __follow_mo...
1083
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1084
1085
  static int follow_dotdot_rcu(struct nameidata *nd)
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1086
  	set_root_rcu(nd);
9875cf806   David Howells   Add a dentry op t...
1087
  	while (1) {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
  		if (nd->path.dentry == nd->root.dentry &&
  		    nd->path.mnt == nd->root.mnt) {
  			break;
  		}
  		if (nd->path.dentry != nd->path.mnt->mnt_root) {
  			struct dentry *old = nd->path.dentry;
  			struct dentry *parent = old->d_parent;
  			unsigned seq;
  
  			seq = read_seqcount_begin(&parent->d_seq);
  			if (read_seqcount_retry(&old->d_seq, nd->seq))
ef7562d52   Al Viro   make handle_dots(...
1099
  				goto failed;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1100
1101
1102
1103
1104
1105
1106
  			nd->path.dentry = parent;
  			nd->seq = seq;
  			break;
  		}
  		if (!follow_up_rcu(&nd->path))
  			break;
  		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1107
  	}
b37199e62   Al Viro   rcuwalk: recheck ...
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
  	while (d_mountpoint(nd->path.dentry)) {
  		struct mount *mounted;
  		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
  		if (!mounted)
  			break;
  		nd->path.mnt = &mounted->mnt;
  		nd->path.dentry = mounted->mnt.mnt_root;
  		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  		if (!read_seqretry(&mount_lock, nd->m_seq))
  			goto failed;
  	}
dea393761   Al Viro   Trim excessive ar...
1119
  	nd->inode = nd->path.dentry->d_inode;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1120
  	return 0;
ef7562d52   Al Viro   make handle_dots(...
1121
1122
1123
  
  failed:
  	nd->flags &= ~LOOKUP_RCU;
5b6ca027d   Al Viro   reduce vfs_path_l...
1124
1125
  	if (!(nd->flags & LOOKUP_ROOT))
  		nd->root.mnt = NULL;
8b61e74ff   Al Viro   get rid of {lock,...
1126
  	rcu_read_unlock();
ef7562d52   Al Viro   make handle_dots(...
1127
  	return -ECHILD;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1128
  }
9875cf806   David Howells   Add a dentry op t...
1129
  /*
cc53ce53c   David Howells   Add a dentry op t...
1130
1131
1132
   * Follow down to the covering mount currently visible to userspace.  At each
   * point, the filesystem owning that dentry may be queried as to whether the
   * caller is permitted to proceed or not.
cc53ce53c   David Howells   Add a dentry op t...
1133
   */
7cc90cc3f   Al Viro   don't pass 'mount...
1134
  int follow_down(struct path *path)
cc53ce53c   David Howells   Add a dentry op t...
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
  {
  	unsigned managed;
  	int ret;
  
  	while (managed = ACCESS_ONCE(path->dentry->d_flags),
  	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  		/* Allow the filesystem to manage the transit without i_mutex
  		 * being held.
  		 *
  		 * We indicate to the filesystem if someone is trying to mount
  		 * something here.  This gives autofs the chance to deny anyone
  		 * other than its daemon the right to mount on its
  		 * superstructure.
  		 *
  		 * The filesystem may sleep at this point.
  		 */
  		if (managed & DCACHE_MANAGE_TRANSIT) {
  			BUG_ON(!path->dentry->d_op);
  			BUG_ON(!path->dentry->d_op->d_manage);
ab90911ff   David Howells   Allow d_manage() ...
1154
  			ret = path->dentry->d_op->d_manage(
1aed3e420   Al Viro   lose 'mounting_he...
1155
  				path->dentry, false);
cc53ce53c   David Howells   Add a dentry op t...
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
  			if (ret < 0)
  				return ret == -EISDIR ? 0 : ret;
  		}
  
  		/* Transit to a mounted filesystem. */
  		if (managed & DCACHE_MOUNTED) {
  			struct vfsmount *mounted = lookup_mnt(path);
  			if (!mounted)
  				break;
  			dput(path->dentry);
  			mntput(path->mnt);
  			path->mnt = mounted;
  			path->dentry = dget(mounted->mnt_root);
  			continue;
  		}
  
  		/* Don't handle automount points here */
  		break;
  	}
  	return 0;
  }
4d3595073   Al Viro   namei.c: move EXP...
1177
  EXPORT_SYMBOL(follow_down);
cc53ce53c   David Howells   Add a dentry op t...
1178
1179
  
  /*
9875cf806   David Howells   Add a dentry op t...
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
   * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
   */
  static void follow_mount(struct path *path)
  {
  	while (d_mountpoint(path->dentry)) {
  		struct vfsmount *mounted = lookup_mnt(path);
  		if (!mounted)
  			break;
  		dput(path->dentry);
  		mntput(path->mnt);
  		path->mnt = mounted;
  		path->dentry = dget(mounted->mnt_root);
  	}
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1194
  static void follow_dotdot(struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1195
  {
2a7378711   Al Viro   Cache root in nam...
1196
  	set_root(nd);
e518ddb7b   Andreas Mohr   [PATCH] fs/namei....
1197

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1198
  	while(1) {
4ac913785   Jan Blunck   Embed a struct pa...
1199
  		struct dentry *old = nd->path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1200

2a7378711   Al Viro   Cache root in nam...
1201
1202
  		if (nd->path.dentry == nd->root.dentry &&
  		    nd->path.mnt == nd->root.mnt) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1203
1204
  			break;
  		}
4ac913785   Jan Blunck   Embed a struct pa...
1205
  		if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd708   Al Viro   Clean follow_dotd...
1206
1207
  			/* rare case of legitimate dget_parent()... */
  			nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1208
1209
1210
  			dput(old);
  			break;
  		}
3088dd708   Al Viro   Clean follow_dotd...
1211
  		if (!follow_up(&nd->path))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1212
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1213
  	}
79ed02261   Al Viro   switch follow_mou...
1214
  	follow_mount(&nd->path);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1215
  	nd->inode = nd->path.dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1216
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1217
  /*
bad611897   Miklos Szeredi   vfs: split __look...
1218
1219
1220
1221
1222
   * This looks up the name in dcache, possibly revalidates the old dentry and
   * allocates a new one if not found or not valid.  In the need_lookup argument
   * returns whether i_op->lookup is necessary.
   *
   * dir->d_inode->i_mutex must be held
baa038907   Nick Piggin   fs: dentry alloca...
1223
   */
bad611897   Miklos Szeredi   vfs: split __look...
1224
  static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
201f956e4   Al Viro   fs/namei.c: don't...
1225
  				    unsigned int flags, bool *need_lookup)
baa038907   Nick Piggin   fs: dentry alloca...
1226
  {
baa038907   Nick Piggin   fs: dentry alloca...
1227
  	struct dentry *dentry;
bad611897   Miklos Szeredi   vfs: split __look...
1228
  	int error;
baa038907   Nick Piggin   fs: dentry alloca...
1229

bad611897   Miklos Szeredi   vfs: split __look...
1230
1231
1232
  	*need_lookup = false;
  	dentry = d_lookup(dir, name);
  	if (dentry) {
39e3c9553   Jeff Layton   vfs: remove DCACH...
1233
  		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
201f956e4   Al Viro   fs/namei.c: don't...
1234
  			error = d_revalidate(dentry, flags);
bad611897   Miklos Szeredi   vfs: split __look...
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
  			if (unlikely(error <= 0)) {
  				if (error < 0) {
  					dput(dentry);
  					return ERR_PTR(error);
  				} else if (!d_invalidate(dentry)) {
  					dput(dentry);
  					dentry = NULL;
  				}
  			}
  		}
  	}
baa038907   Nick Piggin   fs: dentry alloca...
1246

bad611897   Miklos Szeredi   vfs: split __look...
1247
1248
1249
1250
  	if (!dentry) {
  		dentry = d_alloc(dir, name);
  		if (unlikely(!dentry))
  			return ERR_PTR(-ENOMEM);
baa038907   Nick Piggin   fs: dentry alloca...
1251

bad611897   Miklos Szeredi   vfs: split __look...
1252
  		*need_lookup = true;
baa038907   Nick Piggin   fs: dentry alloca...
1253
1254
1255
1256
1257
  	}
  	return dentry;
  }
  
  /*
13a2c3be0   J. Bruce Fields   dcache: fix outda...
1258
1259
   * Call i_op->lookup on the dentry.  The dentry must be negative and
   * unhashed.
bad611897   Miklos Szeredi   vfs: split __look...
1260
1261
   *
   * dir->d_inode->i_mutex must be held
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1262
   */
bad611897   Miklos Szeredi   vfs: split __look...
1263
  static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
72bd866a0   Al Viro   fs/namei.c: don't...
1264
  				  unsigned int flags)
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1265
  {
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1266
1267
1268
  	struct dentry *old;
  
  	/* Don't create child dentry for a dead directory. */
bad611897   Miklos Szeredi   vfs: split __look...
1269
  	if (unlikely(IS_DEADDIR(dir))) {
e188dc02d   Miklos Szeredi   vfs: fix d_inode_...
1270
  		dput(dentry);
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1271
  		return ERR_PTR(-ENOENT);
e188dc02d   Miklos Szeredi   vfs: fix d_inode_...
1272
  	}
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1273

72bd866a0   Al Viro   fs/namei.c: don't...
1274
  	old = dir->i_op->lookup(dir, dentry, flags);
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1275
1276
1277
1278
1279
1280
  	if (unlikely(old)) {
  		dput(dentry);
  		dentry = old;
  	}
  	return dentry;
  }
a32555466   Al Viro   untangling do_loo...
1281
  static struct dentry *__lookup_hash(struct qstr *name,
72bd866a0   Al Viro   fs/namei.c: don't...
1282
  		struct dentry *base, unsigned int flags)
a32555466   Al Viro   untangling do_loo...
1283
  {
bad611897   Miklos Szeredi   vfs: split __look...
1284
  	bool need_lookup;
a32555466   Al Viro   untangling do_loo...
1285
  	struct dentry *dentry;
72bd866a0   Al Viro   fs/namei.c: don't...
1286
  	dentry = lookup_dcache(name, base, flags, &need_lookup);
bad611897   Miklos Szeredi   vfs: split __look...
1287
1288
  	if (!need_lookup)
  		return dentry;
a32555466   Al Viro   untangling do_loo...
1289

72bd866a0   Al Viro   fs/namei.c: don't...
1290
  	return lookup_real(base->d_inode, dentry, flags);
a32555466   Al Viro   untangling do_loo...
1291
  }
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1292
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1293
1294
1295
1296
   *  It's more convoluted than I'd like it to be, but... it's still fairly
   *  small and for now I'd prefer to have fast path as straight as possible.
   *  It _is_ time-critical.
   */
e97cdc87b   Al Viro   lookup_fast: get ...
1297
  static int lookup_fast(struct nameidata *nd,
697f514df   Miklos Szeredi   vfs: split do_loo...
1298
  		       struct path *path, struct inode **inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1299
  {
4ac913785   Jan Blunck   Embed a struct pa...
1300
  	struct vfsmount *mnt = nd->path.mnt;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1301
  	struct dentry *dentry, *parent = nd->path.dentry;
5a18fff20   Al Viro   untangle do_lookup()
1302
1303
  	int need_reval = 1;
  	int status = 1;
9875cf806   David Howells   Add a dentry op t...
1304
  	int err;
3cac260ad   Al Viro   Take hash recalcu...
1305
  	/*
b04f784e5   Nick Piggin   fs: remove extra ...
1306
1307
1308
1309
  	 * Rename seqlock is not required here because in the off chance
  	 * of a false negative due to a concurrent rename, we're going to
  	 * do the non-racy lookup, below.
  	 */
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1310
1311
  	if (nd->flags & LOOKUP_RCU) {
  		unsigned seq;
da53be12b   Linus Torvalds   Don't pass inode ...
1312
  		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
5a18fff20   Al Viro   untangle do_lookup()
1313
1314
  		if (!dentry)
  			goto unlazy;
12f8ad4b0   Linus Torvalds   vfs: clean up __d...
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
  		/*
  		 * This sequence count validates that the inode matches
  		 * the dentry name information from lookup.
  		 */
  		*inode = dentry->d_inode;
  		if (read_seqcount_retry(&dentry->d_seq, seq))
  			return -ECHILD;
  
  		/*
  		 * This sequence count validates that the parent had no
  		 * changes while we did the lookup of the dentry above.
  		 *
  		 * The memory barrier in read_seqcount_begin of child is
  		 *  enough, we can use __read_seqcount_retry here.
  		 */
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1330
1331
  		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  			return -ECHILD;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1332
  		nd->seq = seq;
5a18fff20   Al Viro   untangle do_lookup()
1333

24643087e   Al Viro   in do_lookup() sp...
1334
  		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
4ce16ef3f   Al Viro   fs/namei.c: don't...
1335
  			status = d_revalidate(dentry, nd->flags);
5a18fff20   Al Viro   untangle do_lookup()
1336
1337
1338
1339
1340
  			if (unlikely(status <= 0)) {
  				if (status != -ECHILD)
  					need_reval = 0;
  				goto unlazy;
  			}
24643087e   Al Viro   in do_lookup() sp...
1341
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1342
1343
  		path->mnt = mnt;
  		path->dentry = dentry;
d6e9bd256   Al Viro   Lift the check fo...
1344
1345
1346
1347
1348
  		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
  			goto unlazy;
  		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  			goto unlazy;
  		return 0;
5a18fff20   Al Viro   untangle do_lookup()
1349
  unlazy:
19660af73   Al Viro   consolidate namei...
1350
1351
  		if (unlazy_walk(nd, dentry))
  			return -ECHILD;
5a18fff20   Al Viro   untangle do_lookup()
1352
  	} else {
e97cdc87b   Al Viro   lookup_fast: get ...
1353
  		dentry = __d_lookup(parent, &nd->last);
9875cf806   David Howells   Add a dentry op t...
1354
  	}
5a18fff20   Al Viro   untangle do_lookup()
1355

81e6f5208   Al Viro   untangling do_loo...
1356
1357
  	if (unlikely(!dentry))
  		goto need_lookup;
5a18fff20   Al Viro   untangle do_lookup()
1358
  	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
4ce16ef3f   Al Viro   fs/namei.c: don't...
1359
  		status = d_revalidate(dentry, nd->flags);
5a18fff20   Al Viro   untangle do_lookup()
1360
1361
1362
1363
1364
1365
1366
  	if (unlikely(status <= 0)) {
  		if (status < 0) {
  			dput(dentry);
  			return status;
  		}
  		if (!d_invalidate(dentry)) {
  			dput(dentry);
81e6f5208   Al Viro   untangling do_loo...
1367
  			goto need_lookup;
5a18fff20   Al Viro   untangle do_lookup()
1368
  		}
24643087e   Al Viro   in do_lookup() sp...
1369
  	}
697f514df   Miklos Szeredi   vfs: split do_loo...
1370

9875cf806   David Howells   Add a dentry op t...
1371
1372
1373
  	path->mnt = mnt;
  	path->dentry = dentry;
  	err = follow_managed(path, nd->flags);
893122141   Ian Kent   vfs - fix dentry ...
1374
1375
  	if (unlikely(err < 0)) {
  		path_put_conditional(path, nd);
9875cf806   David Howells   Add a dentry op t...
1376
  		return err;
893122141   Ian Kent   vfs - fix dentry ...
1377
  	}
a3fbbde70   Al Viro   VFS: we need to s...
1378
1379
  	if (err)
  		nd->flags |= LOOKUP_JUMPED;
9875cf806   David Howells   Add a dentry op t...
1380
  	*inode = path->dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1381
  	return 0;
81e6f5208   Al Viro   untangling do_loo...
1382
1383
  
  need_lookup:
697f514df   Miklos Szeredi   vfs: split do_loo...
1384
1385
1386
1387
  	return 1;
  }
  
  /* Fast lookup failed, do it the slow way */
cc2a52711   Al Viro   lookup_slow: get ...
1388
  static int lookup_slow(struct nameidata *nd, struct path *path)
697f514df   Miklos Szeredi   vfs: split do_loo...
1389
1390
1391
1392
1393
  {
  	struct dentry *dentry, *parent;
  	int err;
  
  	parent = nd->path.dentry;
81e6f5208   Al Viro   untangling do_loo...
1394
1395
1396
  	BUG_ON(nd->inode != parent->d_inode);
  
  	mutex_lock(&parent->d_inode->i_mutex);
cc2a52711   Al Viro   lookup_slow: get ...
1397
  	dentry = __lookup_hash(&nd->last, parent, nd->flags);
81e6f5208   Al Viro   untangling do_loo...
1398
1399
1400
  	mutex_unlock(&parent->d_inode->i_mutex);
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
697f514df   Miklos Szeredi   vfs: split do_loo...
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
  	path->mnt = nd->path.mnt;
  	path->dentry = dentry;
  	err = follow_managed(path, nd->flags);
  	if (unlikely(err < 0)) {
  		path_put_conditional(path, nd);
  		return err;
  	}
  	if (err)
  		nd->flags |= LOOKUP_JUMPED;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1411
  }
52094c8a0   Al Viro   take RCU-dependen...
1412
1413
1414
  static inline int may_lookup(struct nameidata *nd)
  {
  	if (nd->flags & LOOKUP_RCU) {
4ad5abb3d   Al Viro   no reason to keep...
1415
  		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a0   Al Viro   take RCU-dependen...
1416
1417
  		if (err != -ECHILD)
  			return err;
19660af73   Al Viro   consolidate namei...
1418
  		if (unlazy_walk(nd, NULL))
52094c8a0   Al Viro   take RCU-dependen...
1419
1420
  			return -ECHILD;
  	}
4ad5abb3d   Al Viro   no reason to keep...
1421
  	return inode_permission(nd->inode, MAY_EXEC);
52094c8a0   Al Viro   take RCU-dependen...
1422
  }
9856fa1b2   Al Viro   pull handling of ...
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
  static inline int handle_dots(struct nameidata *nd, int type)
  {
  	if (type == LAST_DOTDOT) {
  		if (nd->flags & LOOKUP_RCU) {
  			if (follow_dotdot_rcu(nd))
  				return -ECHILD;
  		} else
  			follow_dotdot(nd);
  	}
  	return 0;
  }
951361f95   Al Viro   get rid of the la...
1434
1435
1436
1437
1438
1439
  static void terminate_walk(struct nameidata *nd)
  {
  	if (!(nd->flags & LOOKUP_RCU)) {
  		path_put(&nd->path);
  	} else {
  		nd->flags &= ~LOOKUP_RCU;
5b6ca027d   Al Viro   reduce vfs_path_l...
1440
1441
  		if (!(nd->flags & LOOKUP_ROOT))
  			nd->root.mnt = NULL;
8b61e74ff   Al Viro   get rid of {lock,...
1442
  		rcu_read_unlock();
951361f95   Al Viro   get rid of the la...
1443
1444
  	}
  }
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1445
1446
1447
1448
1449
1450
  /*
   * Do we need to follow links? We _really_ want to be able
   * to do this check without having to look at inode->i_op,
   * so we keep a cache of "no, this doesn't need follow_link"
   * for the common case.
   */
b18825a7c   David Howells   VFS: Put a small ...
1451
  static inline int should_follow_link(struct dentry *dentry, int follow)
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1452
  {
b18825a7c   David Howells   VFS: Put a small ...
1453
  	return unlikely(d_is_symlink(dentry)) ? follow : 0;
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1454
  }
ce57dfc17   Al Viro   pull handling of ...
1455
  static inline int walk_component(struct nameidata *nd, struct path *path,
21b9b0739   Al Viro   get rid of name a...
1456
  		int follow)
ce57dfc17   Al Viro   pull handling of ...
1457
1458
1459
1460
1461
1462
1463
1464
  {
  	struct inode *inode;
  	int err;
  	/*
  	 * "." and ".." are special - ".." especially so because it has
  	 * to be able to know about the current root directory and
  	 * parent relationships.
  	 */
21b9b0739   Al Viro   get rid of name a...
1465
1466
  	if (unlikely(nd->last_type != LAST_NORM))
  		return handle_dots(nd, nd->last_type);
e97cdc87b   Al Viro   lookup_fast: get ...
1467
  	err = lookup_fast(nd, path, &inode);
ce57dfc17   Al Viro   pull handling of ...
1468
  	if (unlikely(err)) {
697f514df   Miklos Szeredi   vfs: split do_loo...
1469
1470
  		if (err < 0)
  			goto out_err;
cc2a52711   Al Viro   lookup_slow: get ...
1471
  		err = lookup_slow(nd, path);
697f514df   Miklos Szeredi   vfs: split do_loo...
1472
1473
1474
1475
  		if (err < 0)
  			goto out_err;
  
  		inode = path->dentry->d_inode;
ce57dfc17   Al Viro   pull handling of ...
1476
  	}
697f514df   Miklos Szeredi   vfs: split do_loo...
1477
1478
1479
  	err = -ENOENT;
  	if (!inode)
  		goto out_path_put;
b18825a7c   David Howells   VFS: Put a small ...
1480
  	if (should_follow_link(path->dentry, follow)) {
19660af73   Al Viro   consolidate namei...
1481
1482
  		if (nd->flags & LOOKUP_RCU) {
  			if (unlikely(unlazy_walk(nd, path->dentry))) {
697f514df   Miklos Szeredi   vfs: split do_loo...
1483
1484
  				err = -ECHILD;
  				goto out_err;
19660af73   Al Viro   consolidate namei...
1485
1486
  			}
  		}
ce57dfc17   Al Viro   pull handling of ...
1487
1488
1489
1490
1491
1492
  		BUG_ON(inode != path->dentry->d_inode);
  		return 1;
  	}
  	path_to_nameidata(path, nd);
  	nd->inode = inode;
  	return 0;
697f514df   Miklos Szeredi   vfs: split do_loo...
1493
1494
1495
1496
1497
1498
  
  out_path_put:
  	path_to_nameidata(path, nd);
  out_err:
  	terminate_walk(nd);
  	return err;
ce57dfc17   Al Viro   pull handling of ...
1499
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1500
  /*
b356379a0   Al Viro   Turn resolution o...
1501
1502
1503
1504
1505
1506
1507
1508
1509
   * This limits recursive symlink follows to 8, while
   * limiting consecutive symlinks to 40.
   *
   * Without that kind of total limit, nasty chains of consecutive
   * symlinks can cause almost arbitrarily long lookups.
   */
  static inline int nested_symlink(struct path *path, struct nameidata *nd)
  {
  	int res;
b356379a0   Al Viro   Turn resolution o...
1510
1511
1512
1513
1514
  	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  		path_put_conditional(path, nd);
  		path_put(&nd->path);
  		return -ELOOP;
  	}
1a4022f88   Erez Zadok   VFS: move BUG_ON ...
1515
  	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
b356379a0   Al Viro   Turn resolution o...
1516
1517
1518
1519
1520
1521
1522
  
  	nd->depth++;
  	current->link_count++;
  
  	do {
  		struct path link = *path;
  		void *cookie;
574197e0d   Al Viro   tidy the trailing...
1523
1524
  
  		res = follow_link(&link, nd, &cookie);
6d7b5aaed   Al Viro   namei.c: let foll...
1525
1526
  		if (res)
  			break;
21b9b0739   Al Viro   get rid of name a...
1527
  		res = walk_component(nd, path, LOOKUP_FOLLOW);
574197e0d   Al Viro   tidy the trailing...
1528
  		put_link(nd, &link, cookie);
b356379a0   Al Viro   Turn resolution o...
1529
1530
1531
1532
1533
1534
1535
1536
  	} while (res > 0);
  
  	current->link_count--;
  	nd->depth--;
  	return res;
  }
  
  /*
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1537
1538
1539
1540
1541
1542
1543
   * We can do the critical dentry name comparison and hashing
   * operations one word at a time, but we are limited to:
   *
   * - Architectures with fast unaligned word accesses. We could
   *   do a "get_unaligned()" if this helps and is sufficiently
   *   fast.
   *
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
   * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
   *   do not trap on the (extremely unlikely) case of a page
   *   crossing operation.
   *
   * - Furthermore, we need an efficient 64-bit compile for the
   *   64-bit case in order to generate the "number of bytes in
   *   the final mask". Again, that could be replaced with a
   *   efficient population count instruction or similar.
   */
  #ifdef CONFIG_DCACHE_WORD_ACCESS
f68e556e2   Linus Torvalds   Make the "word-at...
1554
  #include <asm/word-at-a-time.h>
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1555

f68e556e2   Linus Torvalds   Make the "word-at...
1556
  #ifdef CONFIG_64BIT
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1557
1558
1559
1560
1561
1562
1563
1564
  
  static inline unsigned int fold_hash(unsigned long hash)
  {
  	hash += hash >> (8*sizeof(int));
  	return hash;
  }
  
  #else	/* 32-bit case */
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
  #define fold_hash(x) (x)
  
  #endif
  
  unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  {
  	unsigned long a, mask;
  	unsigned long hash = 0;
  
  	for (;;) {
e419b4cc5   Linus Torvalds   vfs: make word-at...
1575
  		a = load_unaligned_zeropad(name);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1576
1577
1578
  		if (len < sizeof(unsigned long))
  			break;
  		hash += a;
f132c5be0   Al Viro   Fix full_name_has...
1579
  		hash *= 9;
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1580
1581
1582
1583
1584
  		name += sizeof(unsigned long);
  		len -= sizeof(unsigned long);
  		if (!len)
  			goto done;
  	}
a5c21dcef   Will Deacon   dcache: allow wor...
1585
  	mask = bytemask_from_count(len);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1586
1587
1588
1589
1590
  	hash += mask & a;
  done:
  	return fold_hash(hash);
  }
  EXPORT_SYMBOL(full_name_hash);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1591
1592
1593
1594
1595
1596
  /*
   * Calculate the length and hash of the path component, and
   * return the length of the component;
   */
  static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  {
36126f8f2   Linus Torvalds   word-at-a-time: m...
1597
1598
  	unsigned long a, b, adata, bdata, mask, hash, len;
  	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1599
1600
1601
1602
1603
1604
  
  	hash = a = 0;
  	len = -sizeof(unsigned long);
  	do {
  		hash = (hash + a) * 9;
  		len += sizeof(unsigned long);
e419b4cc5   Linus Torvalds   vfs: make word-at...
1605
  		a = load_unaligned_zeropad(name+len);
36126f8f2   Linus Torvalds   word-at-a-time: m...
1606
1607
1608
1609
1610
1611
1612
1613
1614
  		b = a ^ REPEAT_BYTE('/');
  	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  
  	adata = prep_zero_mask(a, adata, &constants);
  	bdata = prep_zero_mask(b, bdata, &constants);
  
  	mask = create_zero_mask(adata | bdata);
  
  	hash += a & zero_bytemask(mask);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1615
  	*hashp = fold_hash(hash);
36126f8f2   Linus Torvalds   word-at-a-time: m...
1616
  	return len + find_zero(mask);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1617
1618
1619
  }
  
  #else
0145acc20   Linus Torvalds   vfs: uninline ful...
1620
1621
1622
1623
1624
1625
1626
  unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  {
  	unsigned long hash = init_name_hash();
  	while (len--)
  		hash = partial_name_hash(*name++, hash);
  	return end_name_hash(hash);
  }
ae942ae71   Linus Torvalds   vfs: export full_...
1627
  EXPORT_SYMBOL(full_name_hash);
0145acc20   Linus Torvalds   vfs: uninline ful...
1628

3ddcd0569   Linus Torvalds   vfs: optimize ino...
1629
  /*
200e9ef7a   Linus Torvalds   vfs: split up nam...
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
   * We know there's a real path component here of at least
   * one character.
   */
  static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  {
  	unsigned long hash = init_name_hash();
  	unsigned long len = 0, c;
  
  	c = (unsigned char)*name;
  	do {
  		len++;
  		hash = partial_name_hash(c, hash);
  		c = (unsigned char)name[len];
  	} while (c && c != '/');
  	*hashp = end_name_hash(hash);
  	return len;
  }
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1647
  #endif
200e9ef7a   Linus Torvalds   vfs: split up nam...
1648
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1649
   * Name resolution.
ea3834d9f   Prasanna Meda   namei: add audit_...
1650
1651
   * This is the basic name resolution function, turning a pathname into
   * the final dentry. We expect 'base' to be positive and a directory.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1652
   *
ea3834d9f   Prasanna Meda   namei: add audit_...
1653
1654
   * Returns 0 and nd will have valid dentry and mnt on success.
   * Returns error and drops reference to input namei data on failure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1655
   */
6de88d729   Al Viro   kill __link_path_...
1656
  static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1657
1658
  {
  	struct path next;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1659
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1660
1661
1662
1663
  	
  	while (*name=='/')
  		name++;
  	if (!*name)
086e183a6   Al Viro   pull dropping RCU...
1664
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1665

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1666
1667
  	/* At this point we know we have a real path component. */
  	for(;;) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1668
  		struct qstr this;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1669
  		long len;
fe479a580   Al Viro   merge component t...
1670
  		int type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1671

52094c8a0   Al Viro   take RCU-dependen...
1672
  		err = may_lookup(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1673
1674
   		if (err)
  			break;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1675
  		len = hash_name(name, &this.hash);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1676
  		this.name = name;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1677
  		this.len = len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1678

fe479a580   Al Viro   merge component t...
1679
  		type = LAST_NORM;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1680
  		if (name[0] == '.') switch (len) {
fe479a580   Al Viro   merge component t...
1681
  			case 2:
200e9ef7a   Linus Torvalds   vfs: split up nam...
1682
  				if (name[1] == '.') {
fe479a580   Al Viro   merge component t...
1683
  					type = LAST_DOTDOT;
16c2cd717   Al Viro   untangle the "nee...
1684
1685
  					nd->flags |= LOOKUP_JUMPED;
  				}
fe479a580   Al Viro   merge component t...
1686
1687
1688
1689
  				break;
  			case 1:
  				type = LAST_DOT;
  		}
5a202bcd7   Al Viro   sanitize pathname...
1690
1691
  		if (likely(type == LAST_NORM)) {
  			struct dentry *parent = nd->path.dentry;
16c2cd717   Al Viro   untangle the "nee...
1692
  			nd->flags &= ~LOOKUP_JUMPED;
5a202bcd7   Al Viro   sanitize pathname...
1693
  			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
da53be12b   Linus Torvalds   Don't pass inode ...
1694
  				err = parent->d_op->d_hash(parent, &this);
5a202bcd7   Al Viro   sanitize pathname...
1695
1696
1697
1698
  				if (err < 0)
  					break;
  			}
  		}
fe479a580   Al Viro   merge component t...
1699

5f4a6a695   Al Viro   link_path_walk():...
1700
1701
  		nd->last = this;
  		nd->last_type = type;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1702
  		if (!name[len])
5f4a6a695   Al Viro   link_path_walk():...
1703
  			return 0;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1704
1705
1706
1707
1708
1709
1710
1711
  		/*
  		 * If it wasn't NUL, we know it was '/'. Skip that
  		 * slash, and continue until no more slashes.
  		 */
  		do {
  			len++;
  		} while (unlikely(name[len] == '/'));
  		if (!name[len])
5f4a6a695   Al Viro   link_path_walk():...
1712
  			return 0;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1713
  		name += len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1714

21b9b0739   Al Viro   get rid of name a...
1715
  		err = walk_component(nd, &next, LOOKUP_FOLLOW);
ce57dfc17   Al Viro   pull handling of ...
1716
1717
  		if (err < 0)
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1718

ce57dfc17   Al Viro   pull handling of ...
1719
  		if (err) {
b356379a0   Al Viro   Turn resolution o...
1720
  			err = nested_symlink(&next, nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1721
  			if (err)
a7472baba   Al Viro   make nameidata_de...
1722
  				return err;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1723
  		}
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
1724
  		if (!d_can_lookup(nd->path.dentry)) {
5f4a6a695   Al Viro   link_path_walk():...
1725
1726
1727
  			err = -ENOTDIR; 
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1728
  	}
951361f95   Al Viro   get rid of the la...
1729
  	terminate_walk(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1730
1731
  	return err;
  }
70e9b3571   Al Viro   get rid of nd->file
1732
1733
  static int path_init(int dfd, const char *name, unsigned int flags,
  		     struct nameidata *nd, struct file **fp)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1734
1735
  {
  	int retval = 0;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1736
1737
  
  	nd->last_type = LAST_ROOT; /* if there are only slashes... */
16c2cd717   Al Viro   untangle the "nee...
1738
  	nd->flags = flags | LOOKUP_JUMPED;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1739
  	nd->depth = 0;
5b6ca027d   Al Viro   reduce vfs_path_l...
1740
  	if (flags & LOOKUP_ROOT) {
b18825a7c   David Howells   VFS: Put a small ...
1741
1742
  		struct dentry *root = nd->root.dentry;
  		struct inode *inode = root->d_inode;
73d049a40   Al Viro   open-style analog...
1743
  		if (*name) {
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
1744
  			if (!d_can_lookup(root))
73d049a40   Al Viro   open-style analog...
1745
1746
1747
1748
1749
  				return -ENOTDIR;
  			retval = inode_permission(inode, MAY_EXEC);
  			if (retval)
  				return retval;
  		}
5b6ca027d   Al Viro   reduce vfs_path_l...
1750
1751
1752
  		nd->path = nd->root;
  		nd->inode = inode;
  		if (flags & LOOKUP_RCU) {
8b61e74ff   Al Viro   get rid of {lock,...
1753
  			rcu_read_lock();
5b6ca027d   Al Viro   reduce vfs_path_l...
1754
  			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
48a066e72   Al Viro   RCU'd vfsmounts
1755
  			nd->m_seq = read_seqbegin(&mount_lock);
5b6ca027d   Al Viro   reduce vfs_path_l...
1756
1757
1758
1759
1760
  		} else {
  			path_get(&nd->path);
  		}
  		return 0;
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1761
  	nd->root.mnt = NULL;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1762

48a066e72   Al Viro   RCU'd vfsmounts
1763
  	nd->m_seq = read_seqbegin(&mount_lock);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1764
  	if (*name=='/') {
e41f7d4ee   Al Viro   merge path_init a...
1765
  		if (flags & LOOKUP_RCU) {
8b61e74ff   Al Viro   get rid of {lock,...
1766
  			rcu_read_lock();
e41f7d4ee   Al Viro   merge path_init a...
1767
1768
1769
1770
1771
1772
  			set_root_rcu(nd);
  		} else {
  			set_root(nd);
  			path_get(&nd->root);
  		}
  		nd->path = nd->root;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1773
  	} else if (dfd == AT_FDCWD) {
e41f7d4ee   Al Viro   merge path_init a...
1774
1775
1776
  		if (flags & LOOKUP_RCU) {
  			struct fs_struct *fs = current->fs;
  			unsigned seq;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1777

8b61e74ff   Al Viro   get rid of {lock,...
1778
  			rcu_read_lock();
c28cc3646   Nick Piggin   fs: fs_struct use...
1779

e41f7d4ee   Al Viro   merge path_init a...
1780
1781
1782
1783
1784
1785
1786
1787
  			do {
  				seq = read_seqcount_begin(&fs->seq);
  				nd->path = fs->pwd;
  				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  			} while (read_seqcount_retry(&fs->seq, seq));
  		} else {
  			get_fs_pwd(current->fs, &nd->path);
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1788
  	} else {
582aa64a0   Jeff Layton   vfs: remove unnee...
1789
  		/* Caller must check execute permissions on the starting path component */
2903ff019   Al Viro   switch simple cas...
1790
  		struct fd f = fdget_raw(dfd);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1791
  		struct dentry *dentry;
2903ff019   Al Viro   switch simple cas...
1792
1793
  		if (!f.file)
  			return -EBADF;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1794

2903ff019   Al Viro   switch simple cas...
1795
  		dentry = f.file->f_path.dentry;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1796

f52e0c113   Al Viro   New AT_... flag: ...
1797
  		if (*name) {
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
1798
  			if (!d_can_lookup(dentry)) {
2903ff019   Al Viro   switch simple cas...
1799
1800
1801
  				fdput(f);
  				return -ENOTDIR;
  			}
f52e0c113   Al Viro   New AT_... flag: ...
1802
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1803

2903ff019   Al Viro   switch simple cas...
1804
  		nd->path = f.file->f_path;
e41f7d4ee   Al Viro   merge path_init a...
1805
  		if (flags & LOOKUP_RCU) {
9c225f265   Linus Torvalds   vfs: atomic f_pos...
1806
  			if (f.flags & FDPUT_FPUT)
2903ff019   Al Viro   switch simple cas...
1807
  				*fp = f.file;
e41f7d4ee   Al Viro   merge path_init a...
1808
  			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
8b61e74ff   Al Viro   get rid of {lock,...
1809
  			rcu_read_lock();
e41f7d4ee   Al Viro   merge path_init a...
1810
  		} else {
2903ff019   Al Viro   switch simple cas...
1811
1812
  			path_get(&nd->path);
  			fdput(f);
e41f7d4ee   Al Viro   merge path_init a...
1813
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1814
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1815

31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1816
  	nd->inode = nd->path.dentry->d_inode;
9b4a9b14a   Al Viro   Preparations to c...
1817
  	return 0;
9b4a9b14a   Al Viro   Preparations to c...
1818
  }
bd92d7fed   Al Viro   Make trailing sym...
1819
1820
1821
1822
1823
1824
  static inline int lookup_last(struct nameidata *nd, struct path *path)
  {
  	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  
  	nd->flags &= ~LOOKUP_PARENT;
21b9b0739   Al Viro   get rid of name a...
1825
  	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
bd92d7fed   Al Viro   Make trailing sym...
1826
  }
9b4a9b14a   Al Viro   Preparations to c...
1827
  /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
ee0827cd6   Al Viro   sanitize path_wal...
1828
  static int path_lookupat(int dfd, const char *name,
9b4a9b14a   Al Viro   Preparations to c...
1829
1830
  				unsigned int flags, struct nameidata *nd)
  {
70e9b3571   Al Viro   get rid of nd->file
1831
  	struct file *base = NULL;
bd92d7fed   Al Viro   Make trailing sym...
1832
1833
  	struct path path;
  	int err;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
  
  	/*
  	 * Path walking is largely split up into 2 different synchronisation
  	 * schemes, rcu-walk and ref-walk (explained in
  	 * Documentation/filesystems/path-lookup.txt). These share much of the
  	 * path walk code, but some things particularly setup, cleanup, and
  	 * following mounts are sufficiently divergent that functions are
  	 * duplicated. Typically there is a function foo(), and its RCU
  	 * analogue, foo_rcu().
  	 *
  	 * -ECHILD is the error number of choice (just to avoid clashes) that
  	 * is returned if some aspect of an rcu-walk fails. Such an error must
  	 * be handled by restarting a traditional ref-walk (which will always
  	 * be able to complete).
  	 */
bd92d7fed   Al Viro   Make trailing sym...
1849
  	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
ee0827cd6   Al Viro   sanitize path_wal...
1850

bd92d7fed   Al Viro   Make trailing sym...
1851
1852
  	if (unlikely(err))
  		return err;
ee0827cd6   Al Viro   sanitize path_wal...
1853
1854
  
  	current->total_link_count = 0;
bd92d7fed   Al Viro   Make trailing sym...
1855
1856
1857
  	err = link_path_walk(name, nd);
  
  	if (!err && !(flags & LOOKUP_PARENT)) {
bd92d7fed   Al Viro   Make trailing sym...
1858
1859
1860
1861
  		err = lookup_last(nd, &path);
  		while (err > 0) {
  			void *cookie;
  			struct path link = path;
800179c9b   Kees Cook   fs: add link rest...
1862
1863
1864
  			err = may_follow_link(&link, nd);
  			if (unlikely(err))
  				break;
bd92d7fed   Al Viro   Make trailing sym...
1865
  			nd->flags |= LOOKUP_PARENT;
574197e0d   Al Viro   tidy the trailing...
1866
  			err = follow_link(&link, nd, &cookie);
6d7b5aaed   Al Viro   namei.c: let foll...
1867
1868
1869
  			if (err)
  				break;
  			err = lookup_last(nd, &path);
574197e0d   Al Viro   tidy the trailing...
1870
  			put_link(nd, &link, cookie);
bd92d7fed   Al Viro   Make trailing sym...
1871
1872
  		}
  	}
ee0827cd6   Al Viro   sanitize path_wal...
1873

9f1fafee9   Al Viro   merge handle_reva...
1874
1875
  	if (!err)
  		err = complete_walk(nd);
bd92d7fed   Al Viro   Make trailing sym...
1876
1877
  
  	if (!err && nd->flags & LOOKUP_DIRECTORY) {
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
1878
  		if (!d_can_lookup(nd->path.dentry)) {
bd92d7fed   Al Viro   Make trailing sym...
1879
  			path_put(&nd->path);
bd23a539d   Al Viro   fix leaks in path...
1880
  			err = -ENOTDIR;
bd92d7fed   Al Viro   Make trailing sym...
1881
1882
  		}
  	}
16c2cd717   Al Viro   untangle the "nee...
1883

70e9b3571   Al Viro   get rid of nd->file
1884
1885
  	if (base)
  		fput(base);
ee0827cd6   Al Viro   sanitize path_wal...
1886

5b6ca027d   Al Viro   reduce vfs_path_l...
1887
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2a7378711   Al Viro   Cache root in nam...
1888
1889
1890
  		path_put(&nd->root);
  		nd->root.mnt = NULL;
  	}
bd92d7fed   Al Viro   Make trailing sym...
1891
  	return err;
ee0827cd6   Al Viro   sanitize path_wal...
1892
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1893

873f1eedc   Jeff Layton   vfs: turn do_path...
1894
  static int filename_lookup(int dfd, struct filename *name,
ee0827cd6   Al Viro   sanitize path_wal...
1895
1896
  				unsigned int flags, struct nameidata *nd)
  {
873f1eedc   Jeff Layton   vfs: turn do_path...
1897
  	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
ee0827cd6   Al Viro   sanitize path_wal...
1898
  	if (unlikely(retval == -ECHILD))
873f1eedc   Jeff Layton   vfs: turn do_path...
1899
  		retval = path_lookupat(dfd, name->name, flags, nd);
ee0827cd6   Al Viro   sanitize path_wal...
1900
  	if (unlikely(retval == -ESTALE))
873f1eedc   Jeff Layton   vfs: turn do_path...
1901
1902
  		retval = path_lookupat(dfd, name->name,
  						flags | LOOKUP_REVAL, nd);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1903

f78570dd6   Jeff Layton   audit: remove unn...
1904
  	if (likely(!retval))
adb5c2473   Jeff Layton   audit: make audit...
1905
  		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
170aa3d02   Ulrich Drepper   [PATCH] namei.c: ...
1906
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1907
  }
873f1eedc   Jeff Layton   vfs: turn do_path...
1908
1909
1910
1911
1912
1913
1914
  static int do_path_lookup(int dfd, const char *name,
  				unsigned int flags, struct nameidata *nd)
  {
  	struct filename filename = { .name = name };
  
  	return filename_lookup(dfd, &filename, flags, nd);
  }
79714f72d   Al Viro   get rid of kern_p...
1915
1916
  /* does lookup, returns the object with parent locked */
  struct dentry *kern_path_locked(const char *name, struct path *path)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
1917
  {
79714f72d   Al Viro   get rid of kern_p...
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
  	struct nameidata nd;
  	struct dentry *d;
  	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
  	if (err)
  		return ERR_PTR(err);
  	if (nd.last_type != LAST_NORM) {
  		path_put(&nd.path);
  		return ERR_PTR(-EINVAL);
  	}
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1e0ea0014   Al Viro   use __lookup_hash...
1928
  	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
79714f72d   Al Viro   get rid of kern_p...
1929
1930
1931
1932
1933
1934
1935
  	if (IS_ERR(d)) {
  		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  		path_put(&nd.path);
  		return d;
  	}
  	*path = nd.path;
  	return d;
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
1936
  }
d18114657   Al Viro   [PATCH] new helpe...
1937
1938
1939
1940
1941
1942
1943
1944
  int kern_path(const char *name, unsigned int flags, struct path *path)
  {
  	struct nameidata nd;
  	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  	if (!res)
  		*path = nd.path;
  	return res;
  }
4d3595073   Al Viro   namei.c: move EXP...
1945
  EXPORT_SYMBOL(kern_path);
d18114657   Al Viro   [PATCH] new helpe...
1946

16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1947
1948
1949
1950
1951
1952
  /**
   * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
   * @dentry:  pointer to dentry of the base directory
   * @mnt: pointer to vfs mount of the base directory
   * @name: pointer to file name
   * @flags: lookup flags
e0a012493   Al Viro   switch vfs_path_l...
1953
   * @path: pointer to struct path to fill
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1954
1955
1956
   */
  int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  		    const char *name, unsigned int flags,
e0a012493   Al Viro   switch vfs_path_l...
1957
  		    struct path *path)
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1958
  {
e0a012493   Al Viro   switch vfs_path_l...
1959
1960
1961
1962
1963
  	struct nameidata nd;
  	int err;
  	nd.root.dentry = dentry;
  	nd.root.mnt = mnt;
  	BUG_ON(flags & LOOKUP_PARENT);
5b6ca027d   Al Viro   reduce vfs_path_l...
1964
  	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
e0a012493   Al Viro   switch vfs_path_l...
1965
1966
1967
1968
  	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  	if (!err)
  		*path = nd.path;
  	return err;
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1969
  }
4d3595073   Al Viro   namei.c: move EXP...
1970
  EXPORT_SYMBOL(vfs_path_lookup);
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1971

057f6c019   James Morris   security: prevent...
1972
1973
1974
1975
1976
  /*
   * Restricted form of lookup. Doesn't follow links, single-component only,
   * needs parent already locked. Doesn't follow mounts.
   * SMP-safe.
   */
eead19115   Christoph Hellwig   partially fix up ...
1977
  static struct dentry *lookup_hash(struct nameidata *nd)
057f6c019   James Morris   security: prevent...
1978
  {
72bd866a0   Al Viro   fs/namei.c: don't...
1979
  	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1980
  }
eead19115   Christoph Hellwig   partially fix up ...
1981
  /**
a6b91919e   Randy Dunlap   fs: fix kernel-do...
1982
   * lookup_one_len - filesystem helper to lookup single pathname component
eead19115   Christoph Hellwig   partially fix up ...
1983
1984
1985
1986
   * @name:	pathname component to lookup
   * @base:	base directory to lookup from
   * @len:	maximum length @len should be interpreted to
   *
a6b91919e   Randy Dunlap   fs: fix kernel-do...
1987
1988
   * Note that this routine is purely a helper for filesystem usage and should
   * not be called by generic code.  Also note that by using this function the
eead19115   Christoph Hellwig   partially fix up ...
1989
1990
1991
   * nameidata argument is passed to the filesystem methods and a filesystem
   * using this helper needs to be prepared for that.
   */
057f6c019   James Morris   security: prevent...
1992
1993
  struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  {
057f6c019   James Morris   security: prevent...
1994
  	struct qstr this;
6a96ba544   Al Viro   kill __lookup_one...
1995
  	unsigned int c;
cda309de2   Miklos Szeredi   vfs: move MAY_EXE...
1996
  	int err;
057f6c019   James Morris   security: prevent...
1997

2f9092e10   David Woodhouse   Fix i_mutex vs. r...
1998
  	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
6a96ba544   Al Viro   kill __lookup_one...
1999
2000
  	this.name = name;
  	this.len = len;
0145acc20   Linus Torvalds   vfs: uninline ful...
2001
  	this.hash = full_name_hash(name, len);
6a96ba544   Al Viro   kill __lookup_one...
2002
2003
  	if (!len)
  		return ERR_PTR(-EACCES);
21d8a15ac   Al Viro   lookup_one_len: d...
2004
2005
2006
2007
  	if (unlikely(name[0] == '.')) {
  		if (len < 2 || (len == 2 && name[1] == '.'))
  			return ERR_PTR(-EACCES);
  	}
6a96ba544   Al Viro   kill __lookup_one...
2008
2009
2010
2011
  	while (len--) {
  		c = *(const unsigned char *)name++;
  		if (c == '/' || c == '\0')
  			return ERR_PTR(-EACCES);
6a96ba544   Al Viro   kill __lookup_one...
2012
  	}
5a202bcd7   Al Viro   sanitize pathname...
2013
2014
2015
2016
2017
  	/*
  	 * See if the low-level filesystem might want
  	 * to use its own hash..
  	 */
  	if (base->d_flags & DCACHE_OP_HASH) {
da53be12b   Linus Torvalds   Don't pass inode ...
2018
  		int err = base->d_op->d_hash(base, &this);
5a202bcd7   Al Viro   sanitize pathname...
2019
2020
2021
  		if (err < 0)
  			return ERR_PTR(err);
  	}
eead19115   Christoph Hellwig   partially fix up ...
2022

cda309de2   Miklos Szeredi   vfs: move MAY_EXE...
2023
2024
2025
  	err = inode_permission(base->d_inode, MAY_EXEC);
  	if (err)
  		return ERR_PTR(err);
72bd866a0   Al Viro   fs/namei.c: don't...
2026
  	return __lookup_hash(&this, base, 0);
057f6c019   James Morris   security: prevent...
2027
  }
4d3595073   Al Viro   namei.c: move EXP...
2028
  EXPORT_SYMBOL(lookup_one_len);
057f6c019   James Morris   security: prevent...
2029

1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
2030
2031
  int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  		 struct path *path, int *empty)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2032
  {
2d8f30380   Al Viro   [PATCH] sanitize ...
2033
  	struct nameidata nd;
91a27b2a7   Jeff Layton   vfs: define struc...
2034
  	struct filename *tmp = getname_flags(name, flags, empty);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2035
  	int err = PTR_ERR(tmp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2036
  	if (!IS_ERR(tmp)) {
2d8f30380   Al Viro   [PATCH] sanitize ...
2037
2038
  
  		BUG_ON(flags & LOOKUP_PARENT);
873f1eedc   Jeff Layton   vfs: turn do_path...
2039
  		err = filename_lookup(dfd, tmp, flags, &nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2040
  		putname(tmp);
2d8f30380   Al Viro   [PATCH] sanitize ...
2041
2042
  		if (!err)
  			*path = nd.path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2043
2044
2045
  	}
  	return err;
  }
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
2046
2047
2048
  int user_path_at(int dfd, const char __user *name, unsigned flags,
  		 struct path *path)
  {
f7493e5d9   Linus Torvalds   vfs: tidy up spar...
2049
  	return user_path_at_empty(dfd, name, flags, path, NULL);
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
2050
  }
4d3595073   Al Viro   namei.c: move EXP...
2051
  EXPORT_SYMBOL(user_path_at);
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
2052

873f1eedc   Jeff Layton   vfs: turn do_path...
2053
2054
2055
2056
2057
2058
  /*
   * NB: most callers don't do anything directly with the reference to the
   *     to struct filename, but the nd->last pointer points into the name string
   *     allocated by getname. So we must hold the reference to it until all
   *     path-walking is complete.
   */
91a27b2a7   Jeff Layton   vfs: define struc...
2059
  static struct filename *
9e790bd65   Jeff Layton   vfs: add a flags ...
2060
2061
  user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
  		 unsigned int flags)
2ad94ae65   Al Viro   [PATCH] new (loca...
2062
  {
91a27b2a7   Jeff Layton   vfs: define struc...
2063
  	struct filename *s = getname(path);
2ad94ae65   Al Viro   [PATCH] new (loca...
2064
  	int error;
9e790bd65   Jeff Layton   vfs: add a flags ...
2065
2066
  	/* only LOOKUP_REVAL is allowed in extra flags */
  	flags &= LOOKUP_REVAL;
2ad94ae65   Al Viro   [PATCH] new (loca...
2067
  	if (IS_ERR(s))
91a27b2a7   Jeff Layton   vfs: define struc...
2068
  		return s;
2ad94ae65   Al Viro   [PATCH] new (loca...
2069

9e790bd65   Jeff Layton   vfs: add a flags ...
2070
  	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
91a27b2a7   Jeff Layton   vfs: define struc...
2071
  	if (error) {
2ad94ae65   Al Viro   [PATCH] new (loca...
2072
  		putname(s);
91a27b2a7   Jeff Layton   vfs: define struc...
2073
2074
  		return ERR_PTR(error);
  	}
2ad94ae65   Al Viro   [PATCH] new (loca...
2075

91a27b2a7   Jeff Layton   vfs: define struc...
2076
  	return s;
2ad94ae65   Al Viro   [PATCH] new (loca...
2077
  }
8033426e6   Jeff Layton   vfs: allow umount...
2078
  /**
197df04c7   Al Viro   rename user_path_...
2079
   * mountpoint_last - look up last component for umount
8033426e6   Jeff Layton   vfs: allow umount...
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
   * @nd:   pathwalk nameidata - currently pointing at parent directory of "last"
   * @path: pointer to container for result
   *
   * This is a special lookup_last function just for umount. In this case, we
   * need to resolve the path without doing any revalidation.
   *
   * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
   * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
   * in almost all cases, this lookup will be served out of the dcache. The only
   * cases where it won't are if nd->last refers to a symlink or the path is
   * bogus and it doesn't exist.
   *
   * Returns:
   * -error: if there was an error during lookup. This includes -ENOENT if the
   *         lookup found a negative dentry. The nd->path reference will also be
   *         put in this case.
   *
   * 0:      if we successfully resolved nd->path and found it to not to be a
   *         symlink that needs to be followed. "path" will also be populated.
   *         The nd->path reference will also be put.
   *
   * 1:      if we successfully resolved nd->last and found it to be a symlink
   *         that needs to be followed. "path" will be populated with the path
   *         to the link, and nd->path will *not* be put.
   */
  static int
197df04c7   Al Viro   rename user_path_...
2106
  mountpoint_last(struct nameidata *nd, struct path *path)
8033426e6   Jeff Layton   vfs: allow umount...
2107
2108
2109
2110
  {
  	int error = 0;
  	struct dentry *dentry;
  	struct dentry *dir = nd->path.dentry;
35759521e   Al Viro   take unlazy_walk(...
2111
2112
2113
2114
2115
2116
  	/* If we're in rcuwalk, drop out of it to handle last component */
  	if (nd->flags & LOOKUP_RCU) {
  		if (unlazy_walk(nd, NULL)) {
  			error = -ECHILD;
  			goto out;
  		}
8033426e6   Jeff Layton   vfs: allow umount...
2117
2118
2119
2120
2121
2122
  	}
  
  	nd->flags &= ~LOOKUP_PARENT;
  
  	if (unlikely(nd->last_type != LAST_NORM)) {
  		error = handle_dots(nd, nd->last_type);
35759521e   Al Viro   take unlazy_walk(...
2123
2124
2125
2126
  		if (error)
  			goto out;
  		dentry = dget(nd->path.dentry);
  		goto done;
8033426e6   Jeff Layton   vfs: allow umount...
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
  	}
  
  	mutex_lock(&dir->d_inode->i_mutex);
  	dentry = d_lookup(dir, &nd->last);
  	if (!dentry) {
  		/*
  		 * No cached dentry. Mounted dentries are pinned in the cache,
  		 * so that means that this dentry is probably a symlink or the
  		 * path doesn't actually point to a mounted dentry.
  		 */
  		dentry = d_alloc(dir, &nd->last);
  		if (!dentry) {
  			error = -ENOMEM;
bcceeeba9   Dave Jones   Add missing unloc...
2140
  			mutex_unlock(&dir->d_inode->i_mutex);
35759521e   Al Viro   take unlazy_walk(...
2141
  			goto out;
8033426e6   Jeff Layton   vfs: allow umount...
2142
  		}
35759521e   Al Viro   take unlazy_walk(...
2143
2144
  		dentry = lookup_real(dir->d_inode, dentry, nd->flags);
  		error = PTR_ERR(dentry);
bcceeeba9   Dave Jones   Add missing unloc...
2145
2146
  		if (IS_ERR(dentry)) {
  			mutex_unlock(&dir->d_inode->i_mutex);
35759521e   Al Viro   take unlazy_walk(...
2147
  			goto out;
bcceeeba9   Dave Jones   Add missing unloc...
2148
  		}
8033426e6   Jeff Layton   vfs: allow umount...
2149
2150
  	}
  	mutex_unlock(&dir->d_inode->i_mutex);
35759521e   Al Viro   take unlazy_walk(...
2151
2152
2153
2154
2155
  done:
  	if (!dentry->d_inode) {
  		error = -ENOENT;
  		dput(dentry);
  		goto out;
8033426e6   Jeff Layton   vfs: allow umount...
2156
  	}
35759521e   Al Viro   take unlazy_walk(...
2157
2158
  	path->dentry = dentry;
  	path->mnt = mntget(nd->path.mnt);
b18825a7c   David Howells   VFS: Put a small ...
2159
  	if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
35759521e   Al Viro   take unlazy_walk(...
2160
2161
2162
2163
  		return 1;
  	follow_mount(path);
  	error = 0;
  out:
8033426e6   Jeff Layton   vfs: allow umount...
2164
2165
2166
2167
2168
  	terminate_walk(nd);
  	return error;
  }
  
  /**
197df04c7   Al Viro   rename user_path_...
2169
   * path_mountpoint - look up a path to be umounted
8033426e6   Jeff Layton   vfs: allow umount...
2170
2171
   * @dfd:	directory file descriptor to start walk from
   * @name:	full pathname to walk
606d6fe3f   Randy Dunlap   fs/namei.c: fix n...
2172
   * @path:	pointer to container for result
8033426e6   Jeff Layton   vfs: allow umount...
2173
   * @flags:	lookup flags
8033426e6   Jeff Layton   vfs: allow umount...
2174
2175
   *
   * Look up the given name, but don't attempt to revalidate the last component.
606d6fe3f   Randy Dunlap   fs/namei.c: fix n...
2176
   * Returns 0 and "path" will be valid on success; Returns error otherwise.
8033426e6   Jeff Layton   vfs: allow umount...
2177
2178
   */
  static int
197df04c7   Al Viro   rename user_path_...
2179
  path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
8033426e6   Jeff Layton   vfs: allow umount...
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
  {
  	struct file *base = NULL;
  	struct nameidata nd;
  	int err;
  
  	err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
  	if (unlikely(err))
  		return err;
  
  	current->total_link_count = 0;
  	err = link_path_walk(name, &nd);
  	if (err)
  		goto out;
197df04c7   Al Viro   rename user_path_...
2193
  	err = mountpoint_last(&nd, path);
8033426e6   Jeff Layton   vfs: allow umount...
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
  	while (err > 0) {
  		void *cookie;
  		struct path link = *path;
  		err = may_follow_link(&link, &nd);
  		if (unlikely(err))
  			break;
  		nd.flags |= LOOKUP_PARENT;
  		err = follow_link(&link, &nd, &cookie);
  		if (err)
  			break;
197df04c7   Al Viro   rename user_path_...
2204
  		err = mountpoint_last(&nd, path);
8033426e6   Jeff Layton   vfs: allow umount...
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
  		put_link(&nd, &link, cookie);
  	}
  out:
  	if (base)
  		fput(base);
  
  	if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
  		path_put(&nd.root);
  
  	return err;
  }
2d8646510   Al Viro   introduce kern_pa...
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
  static int
  filename_mountpoint(int dfd, struct filename *s, struct path *path,
  			unsigned int flags)
  {
  	int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
  	if (unlikely(error == -ECHILD))
  		error = path_mountpoint(dfd, s->name, path, flags);
  	if (unlikely(error == -ESTALE))
  		error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
  	if (likely(!error))
  		audit_inode(s, path->dentry, 0);
  	return error;
  }
8033426e6   Jeff Layton   vfs: allow umount...
2229
  /**
197df04c7   Al Viro   rename user_path_...
2230
   * user_path_mountpoint_at - lookup a path from userland in order to umount it
8033426e6   Jeff Layton   vfs: allow umount...
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
   * @dfd:	directory file descriptor
   * @name:	pathname from userland
   * @flags:	lookup flags
   * @path:	pointer to container to hold result
   *
   * A umount is a special case for path walking. We're not actually interested
   * in the inode in this situation, and ESTALE errors can be a problem. We
   * simply want track down the dentry and vfsmount attached at the mountpoint
   * and avoid revalidating the last component.
   *
   * Returns 0 and populates "path" on success.
   */
  int
197df04c7   Al Viro   rename user_path_...
2244
  user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
8033426e6   Jeff Layton   vfs: allow umount...
2245
2246
2247
2248
  			struct path *path)
  {
  	struct filename *s = getname(name);
  	int error;
8033426e6   Jeff Layton   vfs: allow umount...
2249
2250
  	if (IS_ERR(s))
  		return PTR_ERR(s);
2d8646510   Al Viro   introduce kern_pa...
2251
  	error = filename_mountpoint(dfd, s, path, flags);
8033426e6   Jeff Layton   vfs: allow umount...
2252
2253
2254
  	putname(s);
  	return error;
  }
2d8646510   Al Viro   introduce kern_pa...
2255
2256
2257
2258
2259
2260
2261
2262
  int
  kern_path_mountpoint(int dfd, const char *name, struct path *path,
  			unsigned int flags)
  {
  	struct filename s = {.name = name};
  	return filename_mountpoint(dfd, &s, path, flags);
  }
  EXPORT_SYMBOL(kern_path_mountpoint);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2263
2264
2265
2266
2267
2268
  /*
   * It's inline, so penalty for filesystems that don't use sticky bit is
   * minimal.
   */
  static inline int check_sticky(struct inode *dir, struct inode *inode)
  {
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
2269
  	kuid_t fsuid = current_fsuid();
da9592ede   David Howells   CRED: Wrap task c...
2270

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2271
2272
  	if (!(dir->i_mode & S_ISVTX))
  		return 0;
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
2273
  	if (uid_eq(inode->i_uid, fsuid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2274
  		return 0;
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
2275
  	if (uid_eq(dir->i_uid, fsuid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2276
  		return 0;
1a48e2ac0   Eric W. Biederman   userns: Replace t...
2277
  	return !inode_capable(inode, CAP_FOWNER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
  }
  
  /*
   *	Check whether we can remove a link victim from directory dir, check
   *  whether the type of victim is right.
   *  1. We can't do it if dir is read-only (done in permission())
   *  2. We should have write and exec permissions on dir
   *  3. We can't remove anything from append-only dir
   *  4. We can't do anything with immutable dir (done in permission())
   *  5. If the sticky bit on dir is set we should either
   *	a. be owner of dir, or
   *	b. be owner of victim, or
   *	c. have CAP_FOWNER capability
   *  6. If the victim is append-only or immutable we can't do antyhing with
   *     links pointing to it.
   *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
   *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
   *  9. We can't remove a root or mountpoint.
   * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
   *     nfs_async_unlink().
   */
b18825a7c   David Howells   VFS: Put a small ...
2299
  static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2300
  {
b18825a7c   David Howells   VFS: Put a small ...
2301
  	struct inode *inode = victim->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2302
  	int error;
b18825a7c   David Howells   VFS: Put a small ...
2303
  	if (d_is_negative(victim))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2304
  		return -ENOENT;
b18825a7c   David Howells   VFS: Put a small ...
2305
  	BUG_ON(!inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2306
2307
  
  	BUG_ON(victim->d_parent->d_inode != dir);
4fa6b5ecb   Jeff Layton   audit: overhaul _...
2308
  	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2309

f419a2e3b   Al Viro   [PATCH] kill name...
2310
  	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2311
2312
2313
2314
  	if (error)
  		return error;
  	if (IS_APPEND(dir))
  		return -EPERM;
b18825a7c   David Howells   VFS: Put a small ...
2315
2316
2317
  
  	if (check_sticky(dir, inode) || IS_APPEND(inode) ||
  	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2318
2319
  		return -EPERM;
  	if (isdir) {
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
2320
  		if (!d_is_dir(victim))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2321
2322
2323
  			return -ENOTDIR;
  		if (IS_ROOT(victim))
  			return -EBUSY;
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
2324
  	} else if (d_is_dir(victim))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
  		return -EISDIR;
  	if (IS_DEADDIR(dir))
  		return -ENOENT;
  	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  		return -EBUSY;
  	return 0;
  }
  
  /*	Check whether we can create an object with dentry child in directory
   *  dir.
   *  1. We can't do it if child already exists (open has special treatment for
   *     this case, but since we are inlined it's OK)
   *  2. We can't do it if dir is read-only (done in permission())
   *  3. We should have write and exec permissions on dir
   *  4. We can't do it if dir is immutable (done in permission())
   */
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2341
  static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2342
  {
14e972b45   Jeff Layton   audit: add child ...
2343
  	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2344
2345
2346
2347
  	if (child->d_inode)
  		return -EEXIST;
  	if (IS_DEADDIR(dir))
  		return -ENOENT;
f419a2e3b   Al Viro   [PATCH] kill name...
2348
  	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2349
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2350
2351
2352
2353
2354
2355
2356
2357
  /*
   * p1 and p2 should be directories on the same fs.
   */
  struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  {
  	struct dentry *p;
  
  	if (p1 == p2) {
f2eace23e   Ingo Molnar   [PATCH] lockdep: ...
2358
  		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2359
2360
  		return NULL;
  	}
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
2361
  	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2362

e2761a116   OGAWA Hirofumi   [PATCH vfs-2.6 2/...
2363
2364
2365
2366
2367
  	p = d_ancestor(p2, p1);
  	if (p) {
  		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  		return p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2368
  	}
e2761a116   OGAWA Hirofumi   [PATCH vfs-2.6 2/...
2369
2370
2371
2372
2373
  	p = d_ancestor(p1, p2);
  	if (p) {
  		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  		return p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2374
  	}
f2eace23e   Ingo Molnar   [PATCH] lockdep: ...
2375
2376
  	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2377
2378
  	return NULL;
  }
4d3595073   Al Viro   namei.c: move EXP...
2379
  EXPORT_SYMBOL(lock_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2380
2381
2382
  
  void unlock_rename(struct dentry *p1, struct dentry *p2)
  {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2383
  	mutex_unlock(&p1->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2384
  	if (p1 != p2) {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2385
  		mutex_unlock(&p2->d_inode->i_mutex);
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
2386
  		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2387
2388
  	}
  }
4d3595073   Al Viro   namei.c: move EXP...
2389
  EXPORT_SYMBOL(unlock_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2390

4acdaf27e   Al Viro   switch ->create()...
2391
  int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
312b63fba   Al Viro   don't pass nameid...
2392
  		bool want_excl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2393
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2394
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2395
2396
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
2397
  	if (!dir->i_op->create)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2398
2399
2400
2401
2402
2403
  		return -EACCES;	/* shouldn't it be ENOSYS? */
  	mode &= S_IALLUGO;
  	mode |= S_IFREG;
  	error = security_inode_create(dir, dentry, mode);
  	if (error)
  		return error;
312b63fba   Al Viro   don't pass nameid...
2404
  	error = dir->i_op->create(dir, dentry, mode, want_excl);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
2405
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
2406
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2407
2408
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
2409
  EXPORT_SYMBOL(vfs_create);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2410

73d049a40   Al Viro   open-style analog...
2411
  static int may_open(struct path *path, int acc_mode, int flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2412
  {
3fb64190a   Christoph Hellwig   pass a struct pat...
2413
  	struct dentry *dentry = path->dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2414
2415
  	struct inode *inode = dentry->d_inode;
  	int error;
bcda76524   Al Viro   Allow O_PATH for ...
2416
2417
2418
  	/* O_PATH? */
  	if (!acc_mode)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2419
2420
  	if (!inode)
  		return -ENOENT;
c8fe8f30c   Christoph Hellwig   cleanup may_open
2421
2422
  	switch (inode->i_mode & S_IFMT) {
  	case S_IFLNK:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2423
  		return -ELOOP;
c8fe8f30c   Christoph Hellwig   cleanup may_open
2424
2425
2426
2427
2428
2429
  	case S_IFDIR:
  		if (acc_mode & MAY_WRITE)
  			return -EISDIR;
  		break;
  	case S_IFBLK:
  	case S_IFCHR:
3fb64190a   Christoph Hellwig   pass a struct pat...
2430
  		if (path->mnt->mnt_flags & MNT_NODEV)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2431
  			return -EACCES;
c8fe8f30c   Christoph Hellwig   cleanup may_open
2432
2433
2434
  		/*FALLTHRU*/
  	case S_IFIFO:
  	case S_IFSOCK:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2435
  		flag &= ~O_TRUNC;
c8fe8f30c   Christoph Hellwig   cleanup may_open
2436
  		break;
4a3fd211c   Dave Hansen   [PATCH] r/o bind ...
2437
  	}
b41572e92   Dave Hansen   r/o bind mounts: ...
2438

3fb64190a   Christoph Hellwig   pass a struct pat...
2439
  	error = inode_permission(inode, acc_mode);
b41572e92   Dave Hansen   r/o bind mounts: ...
2440
2441
  	if (error)
  		return error;
6146f0d5e   Mimi Zohar   integrity: IMA hooks
2442

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2443
2444
2445
2446
  	/*
  	 * An append-only file must be opened in append mode for writing.
  	 */
  	if (IS_APPEND(inode)) {
8737c9305   Al Viro   Switch may_open()...
2447
  		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b5212   Al Viro   O_TRUNC open shou...
2448
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2449
  		if (flag & O_TRUNC)
7715b5212   Al Viro   O_TRUNC open shou...
2450
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2451
2452
2453
  	}
  
  	/* O_NOATIME can only be set by the owner or superuser */
2e1496707   Serge E. Hallyn   userns: rename is...
2454
  	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b5212   Al Viro   O_TRUNC open shou...
2455
  		return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2456

f3c7691e8   J. Bruce Fields   leases: fix write...
2457
  	return 0;
7715b5212   Al Viro   O_TRUNC open shou...
2458
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2459

e1181ee65   Jeff Layton   vfs: pass struct ...
2460
  static int handle_truncate(struct file *filp)
7715b5212   Al Viro   O_TRUNC open shou...
2461
  {
e1181ee65   Jeff Layton   vfs: pass struct ...
2462
  	struct path *path = &filp->f_path;
7715b5212   Al Viro   O_TRUNC open shou...
2463
2464
2465
2466
2467
2468
2469
  	struct inode *inode = path->dentry->d_inode;
  	int error = get_write_access(inode);
  	if (error)
  		return error;
  	/*
  	 * Refuse to truncate files with mandatory locks held on them.
  	 */
d7a06983a   Jeff Layton   locks: fix locks_...
2470
  	error = locks_verify_locked(filp);
7715b5212   Al Viro   O_TRUNC open shou...
2471
  	if (!error)
ea0d3ab23   Tetsuo Handa   LSM: Remove unuse...
2472
  		error = security_path_truncate(path);
7715b5212   Al Viro   O_TRUNC open shou...
2473
2474
2475
  	if (!error) {
  		error = do_truncate(path->dentry, 0,
  				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee65   Jeff Layton   vfs: pass struct ...
2476
  				    filp);
7715b5212   Al Viro   O_TRUNC open shou...
2477
2478
  	}
  	put_write_access(inode);
acd0c9351   Mimi Zohar   IMA: update ima_c...
2479
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2480
  }
d57999e15   Dave Hansen   [PATCH] do namei_...
2481
2482
  static inline int open_to_namei_flags(int flag)
  {
8a5e929dd   Al Viro   don't translitera...
2483
2484
  	if ((flag & O_ACCMODE) == 3)
  		flag--;
d57999e15   Dave Hansen   [PATCH] do namei_...
2485
2486
  	return flag;
  }
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
  static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
  {
  	int error = security_path_mknod(dir, dentry, mode, 0);
  	if (error)
  		return error;
  
  	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
  	if (error)
  		return error;
  
  	return security_inode_create(dir->dentry->d_inode, dentry, mode);
  }
1acf0af9b   David Howells   VFS: Fix the bann...
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
  /*
   * Attempt to atomically look up, create and open a file from a negative
   * dentry.
   *
   * Returns 0 if successful.  The file will have been created and attached to
   * @file by the filesystem calling finish_open().
   *
   * Returns 1 if the file was looked up only or didn't need creating.  The
   * caller will need to perform the open themselves.  @path will have been
   * updated to point to the new dentry.  This may be negative.
   *
   * Returns an error code otherwise.
   */
2675a4eb6   Al Viro   fs/namei.c: get d...
2512
2513
2514
  static int atomic_open(struct nameidata *nd, struct dentry *dentry,
  			struct path *path, struct file *file,
  			const struct open_flags *op,
64894cf84   Al Viro   simplify lookup_o...
2515
  			bool got_write, bool need_lookup,
2675a4eb6   Al Viro   fs/namei.c: get d...
2516
  			int *opened)
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2517
2518
2519
2520
2521
2522
  {
  	struct inode *dir =  nd->path.dentry->d_inode;
  	unsigned open_flag = open_to_namei_flags(op->open_flag);
  	umode_t mode;
  	int error;
  	int acc_mode;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2523
2524
  	int create_error = 0;
  	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
116cc0225   Miklos Szeredi   vfs: don't set FI...
2525
  	bool excl;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2526
2527
2528
2529
2530
  
  	BUG_ON(dentry->d_inode);
  
  	/* Don't create child dentry for a dead directory. */
  	if (unlikely(IS_DEADDIR(dir))) {
2675a4eb6   Al Viro   fs/namei.c: get d...
2531
  		error = -ENOENT;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2532
2533
  		goto out;
  	}
62b259d8b   Miklos Szeredi   vfs: atomic_open(...
2534
  	mode = op->mode;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2535
2536
  	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
  		mode &= ~current_umask();
116cc0225   Miklos Szeredi   vfs: don't set FI...
2537
2538
  	excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
  	if (excl)
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2539
  		open_flag &= ~O_TRUNC;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
  
  	/*
  	 * Checking write permission is tricky, bacuse we don't know if we are
  	 * going to actually need it: O_CREAT opens should work as long as the
  	 * file exists.  But checking existence breaks atomicity.  The trick is
  	 * to check access and if not granted clear O_CREAT from the flags.
  	 *
  	 * Another problem is returing the "right" error value (e.g. for an
  	 * O_EXCL open we want to return EEXIST not EROFS).
  	 */
64894cf84   Al Viro   simplify lookup_o...
2550
2551
2552
  	if (((open_flag & (O_CREAT | O_TRUNC)) ||
  	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
  		if (!(open_flag & O_CREAT)) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2553
2554
2555
2556
2557
2558
2559
  			/*
  			 * No O_CREATE -> atomicity not a requirement -> fall
  			 * back to lookup + open
  			 */
  			goto no_open;
  		} else if (open_flag & (O_EXCL | O_TRUNC)) {
  			/* Fall back and fail with the right error */
64894cf84   Al Viro   simplify lookup_o...
2560
  			create_error = -EROFS;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2561
2562
2563
  			goto no_open;
  		} else {
  			/* No side effects, safe to clear O_CREAT */
64894cf84   Al Viro   simplify lookup_o...
2564
  			create_error = -EROFS;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2565
2566
2567
2568
2569
  			open_flag &= ~O_CREAT;
  		}
  	}
  
  	if (open_flag & O_CREAT) {
38227f78a   Miklos Szeredi   vfs: pass right c...
2570
  		error = may_o_create(&nd->path, dentry, mode);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
  		if (error) {
  			create_error = error;
  			if (open_flag & O_EXCL)
  				goto no_open;
  			open_flag &= ~O_CREAT;
  		}
  	}
  
  	if (nd->flags & LOOKUP_DIRECTORY)
  		open_flag |= O_DIRECTORY;
30d904947   Al Viro   kill struct opendata
2581
2582
2583
  	file->f_path.dentry = DENTRY_NOT_SET;
  	file->f_path.mnt = nd->path.mnt;
  	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
47237687d   Al Viro   ->atomic_open() p...
2584
  				      opened);
d95852777   Al Viro   make ->atomic_ope...
2585
  	if (error < 0) {
d95852777   Al Viro   make ->atomic_ope...
2586
2587
  		if (create_error && error == -ENOENT)
  			error = create_error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2588
2589
  		goto out;
  	}
d95852777   Al Viro   make ->atomic_ope...
2590
  	if (error) {	/* returned 1, that is */
30d904947   Al Viro   kill struct opendata
2591
  		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb6   Al Viro   fs/namei.c: get d...
2592
  			error = -EIO;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2593
2594
  			goto out;
  		}
30d904947   Al Viro   kill struct opendata
2595
  		if (file->f_path.dentry) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2596
  			dput(dentry);
30d904947   Al Viro   kill struct opendata
2597
  			dentry = file->f_path.dentry;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2598
  		}
03da633aa   Al Viro   atomic_open: take...
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
  		if (*opened & FILE_CREATED)
  			fsnotify_create(dir, dentry);
  		if (!dentry->d_inode) {
  			WARN_ON(*opened & FILE_CREATED);
  			if (create_error) {
  				error = create_error;
  				goto out;
  			}
  		} else {
  			if (excl && !(*opened & FILE_CREATED)) {
  				error = -EEXIST;
  				goto out;
  			}
62b2ce964   Sage Weil   vfs: fix propagat...
2612
  		}
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2613
2614
2615
2616
2617
2618
2619
  		goto looked_up;
  	}
  
  	/*
  	 * We didn't have the inode before the open, so check open permission
  	 * here.
  	 */
03da633aa   Al Viro   atomic_open: take...
2620
2621
2622
2623
2624
2625
  	acc_mode = op->acc_mode;
  	if (*opened & FILE_CREATED) {
  		WARN_ON(!(open_flag & O_CREAT));
  		fsnotify_create(dir, dentry);
  		acc_mode = MAY_OPEN;
  	}
2675a4eb6   Al Viro   fs/namei.c: get d...
2626
2627
2628
  	error = may_open(&file->f_path, acc_mode, open_flag);
  	if (error)
  		fput(file);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2629
2630
2631
  
  out:
  	dput(dentry);
2675a4eb6   Al Viro   fs/namei.c: get d...
2632
  	return error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2633

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2634
2635
  no_open:
  	if (need_lookup) {
72bd866a0   Al Viro   fs/namei.c: don't...
2636
  		dentry = lookup_real(dir, dentry, nd->flags);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2637
  		if (IS_ERR(dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2638
  			return PTR_ERR(dentry);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2639
2640
2641
  
  		if (create_error) {
  			int open_flag = op->open_flag;
2675a4eb6   Al Viro   fs/namei.c: get d...
2642
  			error = create_error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
  			if ((open_flag & O_EXCL)) {
  				if (!dentry->d_inode)
  					goto out;
  			} else if (!dentry->d_inode) {
  				goto out;
  			} else if ((open_flag & O_TRUNC) &&
  				   S_ISREG(dentry->d_inode->i_mode)) {
  				goto out;
  			}
  			/* will fail later, go on to get the right error */
  		}
  	}
  looked_up:
  	path->dentry = dentry;
  	path->mnt = nd->path.mnt;
2675a4eb6   Al Viro   fs/namei.c: get d...
2658
  	return 1;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2659
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2660
  /*
1acf0af9b   David Howells   VFS: Fix the bann...
2661
   * Look up and maybe create and open the last component.
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2662
2663
2664
   *
   * Must be called with i_mutex held on parent.
   *
1acf0af9b   David Howells   VFS: Fix the bann...
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
   * Returns 0 if the file was successfully atomically created (if necessary) and
   * opened.  In this case the file will be returned attached to @file.
   *
   * Returns 1 if the file was not completely opened at this time, though lookups
   * and creations will have been performed and the dentry returned in @path will
   * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
   * specified then a negative dentry may be returned.
   *
   * An error code is returned otherwise.
   *
   * FILE_CREATE will be set in @*opened if the dentry was created and will be
   * cleared otherwise prior to returning.
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2677
   */
2675a4eb6   Al Viro   fs/namei.c: get d...
2678
2679
2680
  static int lookup_open(struct nameidata *nd, struct path *path,
  			struct file *file,
  			const struct open_flags *op,
64894cf84   Al Viro   simplify lookup_o...
2681
  			bool got_write, int *opened)
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2682
2683
  {
  	struct dentry *dir = nd->path.dentry;
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2684
  	struct inode *dir_inode = dir->d_inode;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2685
2686
  	struct dentry *dentry;
  	int error;
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2687
  	bool need_lookup;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2688

47237687d   Al Viro   ->atomic_open() p...
2689
  	*opened &= ~FILE_CREATED;
201f956e4   Al Viro   fs/namei.c: don't...
2690
  	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2691
  	if (IS_ERR(dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2692
  		return PTR_ERR(dentry);
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2693

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2694
2695
2696
2697
2698
  	/* Cached positive dentry: will open in f_op->open */
  	if (!need_lookup && dentry->d_inode)
  		goto out_no_open;
  
  	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
64894cf84   Al Viro   simplify lookup_o...
2699
  		return atomic_open(nd, dentry, path, file, op, got_write,
47237687d   Al Viro   ->atomic_open() p...
2700
  				   need_lookup, opened);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2701
  	}
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2702
2703
  	if (need_lookup) {
  		BUG_ON(dentry->d_inode);
72bd866a0   Al Viro   fs/namei.c: don't...
2704
  		dentry = lookup_real(dir_inode, dentry, nd->flags);
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2705
  		if (IS_ERR(dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2706
  			return PTR_ERR(dentry);
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2707
  	}
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
  	/* Negative dentry, just create the file */
  	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
  		umode_t mode = op->mode;
  		if (!IS_POSIXACL(dir->d_inode))
  			mode &= ~current_umask();
  		/*
  		 * This write is needed to ensure that a
  		 * rw->ro transition does not occur between
  		 * the time when the file is created and when
  		 * a permanent write count is taken through
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2718
  		 * the 'struct file' in finish_open().
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2719
  		 */
64894cf84   Al Viro   simplify lookup_o...
2720
2721
  		if (!got_write) {
  			error = -EROFS;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2722
  			goto out_dput;
64894cf84   Al Viro   simplify lookup_o...
2723
  		}
47237687d   Al Viro   ->atomic_open() p...
2724
  		*opened |= FILE_CREATED;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2725
2726
2727
  		error = security_path_mknod(&nd->path, dentry, mode, 0);
  		if (error)
  			goto out_dput;
312b63fba   Al Viro   don't pass nameid...
2728
2729
  		error = vfs_create(dir->d_inode, dentry, mode,
  				   nd->flags & LOOKUP_EXCL);
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2730
2731
2732
  		if (error)
  			goto out_dput;
  	}
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2733
  out_no_open:
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2734
2735
  	path->dentry = dentry;
  	path->mnt = nd->path.mnt;
2675a4eb6   Al Viro   fs/namei.c: get d...
2736
  	return 1;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2737
2738
2739
  
  out_dput:
  	dput(dentry);
2675a4eb6   Al Viro   fs/namei.c: get d...
2740
  	return error;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2741
2742
2743
  }
  
  /*
fe2d35ff0   Al Viro   switch non-create...
2744
   * Handle the last step of open()
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2745
   */
2675a4eb6   Al Viro   fs/namei.c: get d...
2746
2747
  static int do_last(struct nameidata *nd, struct path *path,
  		   struct file *file, const struct open_flags *op,
669abf4e5   Jeff Layton   vfs: make path_op...
2748
  		   int *opened, struct filename *name)
fb1cc555d   Al Viro   gut do_filp_open(...
2749
  {
a1e28038d   Al Viro   pull the common p...
2750
  	struct dentry *dir = nd->path.dentry;
ca344a894   Al Viro   do_last: unify ma...
2751
  	int open_flag = op->open_flag;
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2752
  	bool will_truncate = (open_flag & O_TRUNC) != 0;
64894cf84   Al Viro   simplify lookup_o...
2753
  	bool got_write = false;
bcda76524   Al Viro   Allow O_PATH for ...
2754
  	int acc_mode = op->acc_mode;
a1eb33153   Miklos Szeredi   vfs: do_last(): i...
2755
  	struct inode *inode;
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2756
  	bool symlink_ok = false;
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2757
2758
  	struct path save_parent = { .dentry = NULL, .mnt = NULL };
  	bool retried = false;
16c2cd717   Al Viro   untangle the "nee...
2759
  	int error;
1f36f774b   Al Viro   Switch !O_CREAT c...
2760

c3e380b0b   Al Viro   Collect "operatio...
2761
2762
  	nd->flags &= ~LOOKUP_PARENT;
  	nd->flags |= op->intent;
bc77daa78   Al Viro   do_last(): fix mi...
2763
  	if (nd->last_type != LAST_NORM) {
fe2d35ff0   Al Viro   switch non-create...
2764
2765
  		error = handle_dots(nd, nd->last_type);
  		if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2766
  			return error;
e83db1672   Miklos Szeredi   vfs: do_last(): c...
2767
  		goto finish_open;
1f36f774b   Al Viro   Switch !O_CREAT c...
2768
  	}
67ee3ad21   Al Viro   Pull handling of ...
2769

ca344a894   Al Viro   do_last: unify ma...
2770
  	if (!(open_flag & O_CREAT)) {
fe2d35ff0   Al Viro   switch non-create...
2771
2772
  		if (nd->last.name[nd->last.len])
  			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
bcda76524   Al Viro   Allow O_PATH for ...
2773
  		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2774
  			symlink_ok = true;
fe2d35ff0   Al Viro   switch non-create...
2775
  		/* we _can_ be in RCU mode here */
e97cdc87b   Al Viro   lookup_fast: get ...
2776
  		error = lookup_fast(nd, path, &inode);
715748654   Miklos Szeredi   vfs: do_last(): c...
2777
2778
2779
2780
  		if (likely(!error))
  			goto finish_lookup;
  
  		if (error < 0)
2675a4eb6   Al Viro   fs/namei.c: get d...
2781
  			goto out;
715748654   Miklos Szeredi   vfs: do_last(): c...
2782
2783
  
  		BUG_ON(nd->inode != dir->d_inode);
b6183df7b   Miklos Szeredi   vfs: do_last(): s...
2784
2785
2786
2787
2788
2789
2790
2791
2792
  	} else {
  		/* create side of things */
  		/*
  		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
  		 * has been cleared when we got to the last component we are
  		 * about to look up
  		 */
  		error = complete_walk(nd);
  		if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2793
  			return error;
fe2d35ff0   Al Viro   switch non-create...
2794

33e2208ac   Jeff Layton   audit: vfs: fix a...
2795
  		audit_inode(name, dir, LOOKUP_PARENT);
b6183df7b   Miklos Szeredi   vfs: do_last(): s...
2796
2797
2798
  		error = -EISDIR;
  		/* trailing slashes? */
  		if (nd->last.name[nd->last.len])
2675a4eb6   Al Viro   fs/namei.c: get d...
2799
  			goto out;
b6183df7b   Miklos Szeredi   vfs: do_last(): s...
2800
  	}
a2c36b450   Al Viro   pull more into do...
2801

16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2802
  retry_lookup:
64894cf84   Al Viro   simplify lookup_o...
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
  	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
  		error = mnt_want_write(nd->path.mnt);
  		if (!error)
  			got_write = true;
  		/*
  		 * do _not_ fail yet - we might not need that or fail with
  		 * a different error; let lookup_open() decide; we'll be
  		 * dropping this one anyway.
  		 */
  	}
a1e28038d   Al Viro   pull the common p...
2813
  	mutex_lock(&dir->d_inode->i_mutex);
64894cf84   Al Viro   simplify lookup_o...
2814
  	error = lookup_open(nd, path, file, op, got_write, opened);
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2815
  	mutex_unlock(&dir->d_inode->i_mutex);
a1e28038d   Al Viro   pull the common p...
2816

2675a4eb6   Al Viro   fs/namei.c: get d...
2817
2818
  	if (error <= 0) {
  		if (error)
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2819
  			goto out;
47237687d   Al Viro   ->atomic_open() p...
2820
  		if ((*opened & FILE_CREATED) ||
496ad9aa8   Al Viro   new helper: file_...
2821
  		    !S_ISREG(file_inode(file)->i_mode))
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2822
  			will_truncate = false;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2823

adb5c2473   Jeff Layton   audit: make audit...
2824
  		audit_inode(name, file->f_path.dentry, 0);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2825
2826
  		goto opened;
  	}
fb1cc555d   Al Viro   gut do_filp_open(...
2827

47237687d   Al Viro   ->atomic_open() p...
2828
  	if (*opened & FILE_CREATED) {
9b44f1b39   Al Viro   move may_open() f...
2829
  		/* Don't check for write permission, don't truncate */
ca344a894   Al Viro   do_last: unify ma...
2830
  		open_flag &= ~O_TRUNC;
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2831
  		will_truncate = false;
bcda76524   Al Viro   Allow O_PATH for ...
2832
  		acc_mode = MAY_OPEN;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2833
  		path_to_nameidata(path, nd);
e83db1672   Miklos Szeredi   vfs: do_last(): c...
2834
  		goto finish_open_created;
fb1cc555d   Al Viro   gut do_filp_open(...
2835
2836
2837
  	}
  
  	/*
3134f37e9   Jeff Layton   vfs: don't let do...
2838
  	 * create/update audit record if it already exists.
fb1cc555d   Al Viro   gut do_filp_open(...
2839
  	 */
b18825a7c   David Howells   VFS: Put a small ...
2840
  	if (d_is_positive(path->dentry))
adb5c2473   Jeff Layton   audit: make audit...
2841
  		audit_inode(name, path->dentry, 0);
fb1cc555d   Al Viro   gut do_filp_open(...
2842

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2843
2844
2845
2846
2847
  	/*
  	 * If atomic_open() acquired write access it is dropped now due to
  	 * possible mount and symlink following (this might be optimized away if
  	 * necessary...)
  	 */
64894cf84   Al Viro   simplify lookup_o...
2848
  	if (got_write) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2849
  		mnt_drop_write(nd->path.mnt);
64894cf84   Al Viro   simplify lookup_o...
2850
  		got_write = false;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2851
  	}
fb1cc555d   Al Viro   gut do_filp_open(...
2852
  	error = -EEXIST;
f8310c592   Al Viro   fix O_EXCL handli...
2853
  	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
fb1cc555d   Al Viro   gut do_filp_open(...
2854
  		goto exit_dput;
9875cf806   David Howells   Add a dentry op t...
2855
2856
2857
  	error = follow_managed(path, nd->flags);
  	if (error < 0)
  		goto exit_dput;
fb1cc555d   Al Viro   gut do_filp_open(...
2858

a3fbbde70   Al Viro   VFS: we need to s...
2859
2860
  	if (error)
  		nd->flags |= LOOKUP_JUMPED;
decf34008   Miklos Szeredi   vfs: do_last(): u...
2861
2862
  	BUG_ON(nd->flags & LOOKUP_RCU);
  	inode = path->dentry->d_inode;
5f5daac12   Miklos Szeredi   vfs: do_last() co...
2863
2864
  finish_lookup:
  	/* we _can_ be in RCU mode here */
fb1cc555d   Al Viro   gut do_filp_open(...
2865
  	error = -ENOENT;
b18825a7c   David Howells   VFS: Put a small ...
2866
  	if (d_is_negative(path->dentry)) {
54c33e7f9   Miklos Szeredi   vfs: do_last(): m...
2867
  		path_to_nameidata(path, nd);
2675a4eb6   Al Viro   fs/namei.c: get d...
2868
  		goto out;
54c33e7f9   Miklos Szeredi   vfs: do_last(): m...
2869
  	}
9e67f3616   Al Viro   Kill is_link argu...
2870

b18825a7c   David Howells   VFS: Put a small ...
2871
  	if (should_follow_link(path->dentry, !symlink_ok)) {
d45ea8679   Miklos Szeredi   vfs: make follow_...
2872
2873
2874
  		if (nd->flags & LOOKUP_RCU) {
  			if (unlikely(unlazy_walk(nd, path->dentry))) {
  				error = -ECHILD;
2675a4eb6   Al Viro   fs/namei.c: get d...
2875
  				goto out;
d45ea8679   Miklos Szeredi   vfs: make follow_...
2876
2877
2878
  			}
  		}
  		BUG_ON(inode != path->dentry->d_inode);
2675a4eb6   Al Viro   fs/namei.c: get d...
2879
  		return 1;
d45ea8679   Miklos Szeredi   vfs: make follow_...
2880
  	}
fb1cc555d   Al Viro   gut do_filp_open(...
2881

16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2882
2883
2884
2885
2886
2887
2888
2889
  	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
  		path_to_nameidata(path, nd);
  	} else {
  		save_parent.dentry = nd->path.dentry;
  		save_parent.mnt = mntget(path->mnt);
  		nd->path.dentry = path->dentry;
  
  	}
decf34008   Miklos Szeredi   vfs: do_last(): u...
2890
  	nd->inode = inode;
a3fbbde70   Al Viro   VFS: we need to s...
2891
  	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
bc77daa78   Al Viro   do_last(): fix mi...
2892
  finish_open:
a3fbbde70   Al Viro   VFS: we need to s...
2893
  	error = complete_walk(nd);
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2894
2895
  	if (error) {
  		path_put(&save_parent);
2675a4eb6   Al Viro   fs/namei.c: get d...
2896
  		return error;
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2897
  	}
bc77daa78   Al Viro   do_last(): fix mi...
2898
  	audit_inode(name, nd->path.dentry, 0);
fb1cc555d   Al Viro   gut do_filp_open(...
2899
  	error = -EISDIR;
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
2900
  	if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2901
  		goto out;
af2f55426   Miklos Szeredi   vfs: do_last(): c...
2902
  	error = -ENOTDIR;
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
2903
  	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2904
  		goto out;
6c0d46c49   Al Viro   fold __open_namei...
2905
  	if (!S_ISREG(nd->inode->i_mode))
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2906
  		will_truncate = false;
6c0d46c49   Al Viro   fold __open_namei...
2907

0f9d1a10c   Al Viro   expand finish_ope...
2908
2909
2910
  	if (will_truncate) {
  		error = mnt_want_write(nd->path.mnt);
  		if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2911
  			goto out;
64894cf84   Al Viro   simplify lookup_o...
2912
  		got_write = true;
0f9d1a10c   Al Viro   expand finish_ope...
2913
  	}
e83db1672   Miklos Szeredi   vfs: do_last(): c...
2914
  finish_open_created:
bcda76524   Al Viro   Allow O_PATH for ...
2915
  	error = may_open(&nd->path, acc_mode, open_flag);
ca344a894   Al Viro   do_last: unify ma...
2916
  	if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2917
  		goto out;
30d904947   Al Viro   kill struct opendata
2918
2919
2920
  	file->f_path.mnt = nd->path.mnt;
  	error = finish_open(file, nd->path.dentry, NULL, opened);
  	if (error) {
30d904947   Al Viro   kill struct opendata
2921
  		if (error == -EOPENSTALE)
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2922
  			goto stale_open;
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2923
  		goto out;
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2924
  	}
a8277b9ba   Miklos Szeredi   vfs: move O_DIREC...
2925
  opened:
2675a4eb6   Al Viro   fs/namei.c: get d...
2926
  	error = open_check_o_direct(file);
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2927
2928
  	if (error)
  		goto exit_fput;
2675a4eb6   Al Viro   fs/namei.c: get d...
2929
  	error = ima_file_check(file, op->acc_mode);
aa4caadb7   Miklos Szeredi   vfs: do_last(): c...
2930
2931
2932
2933
  	if (error)
  		goto exit_fput;
  
  	if (will_truncate) {
2675a4eb6   Al Viro   fs/namei.c: get d...
2934
  		error = handle_truncate(file);
aa4caadb7   Miklos Szeredi   vfs: do_last(): c...
2935
2936
  		if (error)
  			goto exit_fput;
0f9d1a10c   Al Viro   expand finish_ope...
2937
  	}
ca344a894   Al Viro   do_last: unify ma...
2938
  out:
64894cf84   Al Viro   simplify lookup_o...
2939
  	if (got_write)
0f9d1a10c   Al Viro   expand finish_ope...
2940
  		mnt_drop_write(nd->path.mnt);
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2941
  	path_put(&save_parent);
e276ae672   Miklos Szeredi   vfs: do_last(): m...
2942
  	terminate_walk(nd);
2675a4eb6   Al Viro   fs/namei.c: get d...
2943
  	return error;
fb1cc555d   Al Viro   gut do_filp_open(...
2944

fb1cc555d   Al Viro   gut do_filp_open(...
2945
2946
  exit_dput:
  	path_put_conditional(path, nd);
ca344a894   Al Viro   do_last: unify ma...
2947
  	goto out;
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2948
  exit_fput:
2675a4eb6   Al Viro   fs/namei.c: get d...
2949
2950
  	fput(file);
  	goto out;
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2951

f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
  stale_open:
  	/* If no saved parent or already retried then can't retry */
  	if (!save_parent.dentry || retried)
  		goto out;
  
  	BUG_ON(save_parent.dentry != dir);
  	path_put(&nd->path);
  	nd->path = save_parent;
  	nd->inode = dir->d_inode;
  	save_parent.mnt = NULL;
  	save_parent.dentry = NULL;
64894cf84   Al Viro   simplify lookup_o...
2963
  	if (got_write) {
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2964
  		mnt_drop_write(nd->path.mnt);
64894cf84   Al Viro   simplify lookup_o...
2965
  		got_write = false;
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2966
2967
2968
  	}
  	retried = true;
  	goto retry_lookup;
fb1cc555d   Al Viro   gut do_filp_open(...
2969
  }
60545d0d4   Al Viro   [O_TMPFILE] it's ...
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
  static int do_tmpfile(int dfd, struct filename *pathname,
  		struct nameidata *nd, int flags,
  		const struct open_flags *op,
  		struct file *file, int *opened)
  {
  	static const struct qstr name = QSTR_INIT("/", 1);
  	struct dentry *dentry, *child;
  	struct inode *dir;
  	int error = path_lookupat(dfd, pathname->name,
  				  flags | LOOKUP_DIRECTORY, nd);
  	if (unlikely(error))
  		return error;
  	error = mnt_want_write(nd->path.mnt);
  	if (unlikely(error))
  		goto out;
  	/* we want directory to be writable */
  	error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
  	if (error)
  		goto out2;
  	dentry = nd->path.dentry;
  	dir = dentry->d_inode;
  	if (!dir->i_op->tmpfile) {
  		error = -EOPNOTSUPP;
  		goto out2;
  	}
  	child = d_alloc(dentry, &name);
  	if (unlikely(!child)) {
  		error = -ENOMEM;
  		goto out2;
  	}
  	nd->flags &= ~LOOKUP_DIRECTORY;
  	nd->flags |= op->intent;
  	dput(nd->path.dentry);
  	nd->path.dentry = child;
  	error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
  	if (error)
  		goto out2;
  	audit_inode(pathname, nd->path.dentry, 0);
  	error = may_open(&nd->path, op->acc_mode, op->open_flag);
  	if (error)
  		goto out2;
  	file->f_path.mnt = nd->path.mnt;
  	error = finish_open(file, nd->path.dentry, NULL, opened);
  	if (error)
  		goto out2;
  	error = open_check_o_direct(file);
f4e0c30c1   Al Viro   allow the temp fi...
3016
  	if (error) {
60545d0d4   Al Viro   [O_TMPFILE] it's ...
3017
  		fput(file);
f4e0c30c1   Al Viro   allow the temp fi...
3018
3019
3020
3021
3022
3023
  	} else if (!(op->open_flag & O_EXCL)) {
  		struct inode *inode = file_inode(file);
  		spin_lock(&inode->i_lock);
  		inode->i_state |= I_LINKABLE;
  		spin_unlock(&inode->i_lock);
  	}
60545d0d4   Al Viro   [O_TMPFILE] it's ...
3024
3025
3026
3027
3028
3029
  out2:
  	mnt_drop_write(nd->path.mnt);
  out:
  	path_put(&nd->path);
  	return error;
  }
669abf4e5   Jeff Layton   vfs: make path_op...
3030
  static struct file *path_openat(int dfd, struct filename *pathname,
73d049a40   Al Viro   open-style analog...
3031
  		struct nameidata *nd, const struct open_flags *op, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3032
  {
fe2d35ff0   Al Viro   switch non-create...
3033
  	struct file *base = NULL;
30d904947   Al Viro   kill struct opendata
3034
  	struct file *file;
9850c0565   Al Viro   Fix the -ESTALE h...
3035
  	struct path path;
47237687d   Al Viro   ->atomic_open() p...
3036
  	int opened = 0;
13aab428a   Al Viro   separate -ESTALE/...
3037
  	int error;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
3038

30d904947   Al Viro   kill struct opendata
3039
  	file = get_empty_filp();
1afc99bea   Al Viro   propagate error f...
3040
3041
  	if (IS_ERR(file))
  		return file;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
3042

30d904947   Al Viro   kill struct opendata
3043
  	file->f_flags = op->open_flag;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
3044

bb458c644   Al Viro   Safer ABI for O_T...
3045
  	if (unlikely(file->f_flags & __O_TMPFILE)) {
60545d0d4   Al Viro   [O_TMPFILE] it's ...
3046
3047
3048
  		error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
  		goto out;
  	}
669abf4e5   Jeff Layton   vfs: make path_op...
3049
  	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
3050
  	if (unlikely(error))
2675a4eb6   Al Viro   fs/namei.c: get d...
3051
  		goto out;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
3052

fe2d35ff0   Al Viro   switch non-create...
3053
  	current->total_link_count = 0;
669abf4e5   Jeff Layton   vfs: make path_op...
3054
  	error = link_path_walk(pathname->name, nd);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
3055
  	if (unlikely(error))
2675a4eb6   Al Viro   fs/namei.c: get d...
3056
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3057

2675a4eb6   Al Viro   fs/namei.c: get d...
3058
3059
  	error = do_last(nd, &path, file, op, &opened, pathname);
  	while (unlikely(error > 0)) { /* trailing symlink */
7b9337aaf   Nick Piggin   fs: namei fix ->p...
3060
  		struct path link = path;
def4af30c   Al Viro   Get rid of symlin...
3061
  		void *cookie;
574197e0d   Al Viro   tidy the trailing...
3062
  		if (!(nd->flags & LOOKUP_FOLLOW)) {
73d049a40   Al Viro   open-style analog...
3063
3064
  			path_put_conditional(&path, nd);
  			path_put(&nd->path);
2675a4eb6   Al Viro   fs/namei.c: get d...
3065
  			error = -ELOOP;
40b39136f   Al Viro   path_openat: clea...
3066
3067
  			break;
  		}
800179c9b   Kees Cook   fs: add link rest...
3068
3069
3070
  		error = may_follow_link(&link, nd);
  		if (unlikely(error))
  			break;
73d049a40   Al Viro   open-style analog...
3071
3072
  		nd->flags |= LOOKUP_PARENT;
  		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
574197e0d   Al Viro   tidy the trailing...
3073
  		error = follow_link(&link, nd, &cookie);
c3e380b0b   Al Viro   Collect "operatio...
3074
  		if (unlikely(error))
2675a4eb6   Al Viro   fs/namei.c: get d...
3075
3076
  			break;
  		error = do_last(nd, &path, file, op, &opened, pathname);
574197e0d   Al Viro   tidy the trailing...
3077
  		put_link(nd, &link, cookie);
806b681cb   Al Viro   Turn do_link spag...
3078
  	}
10fa8e62f   Al Viro   Unify exits in O_...
3079
  out:
73d049a40   Al Viro   open-style analog...
3080
3081
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  		path_put(&nd->root);
fe2d35ff0   Al Viro   switch non-create...
3082
3083
  	if (base)
  		fput(base);
2675a4eb6   Al Viro   fs/namei.c: get d...
3084
3085
  	if (!(opened & FILE_OPENED)) {
  		BUG_ON(!error);
30d904947   Al Viro   kill struct opendata
3086
  		put_filp(file);
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
3087
  	}
2675a4eb6   Al Viro   fs/namei.c: get d...
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
  	if (unlikely(error)) {
  		if (error == -EOPENSTALE) {
  			if (flags & LOOKUP_RCU)
  				error = -ECHILD;
  			else
  				error = -ESTALE;
  		}
  		file = ERR_PTR(error);
  	}
  	return file;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3098
  }
669abf4e5   Jeff Layton   vfs: make path_op...
3099
  struct file *do_filp_open(int dfd, struct filename *pathname,
f9652e10c   Al Viro   allow build_open_...
3100
  		const struct open_flags *op)
13aab428a   Al Viro   separate -ESTALE/...
3101
  {
73d049a40   Al Viro   open-style analog...
3102
  	struct nameidata nd;
f9652e10c   Al Viro   allow build_open_...
3103
  	int flags = op->lookup_flags;
13aab428a   Al Viro   separate -ESTALE/...
3104
  	struct file *filp;
73d049a40   Al Viro   open-style analog...
3105
  	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
13aab428a   Al Viro   separate -ESTALE/...
3106
  	if (unlikely(filp == ERR_PTR(-ECHILD)))
73d049a40   Al Viro   open-style analog...
3107
  		filp = path_openat(dfd, pathname, &nd, op, flags);
13aab428a   Al Viro   separate -ESTALE/...
3108
  	if (unlikely(filp == ERR_PTR(-ESTALE)))
73d049a40   Al Viro   open-style analog...
3109
  		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
13aab428a   Al Viro   separate -ESTALE/...
3110
3111
  	return filp;
  }
73d049a40   Al Viro   open-style analog...
3112
  struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
f9652e10c   Al Viro   allow build_open_...
3113
  		const char *name, const struct open_flags *op)
73d049a40   Al Viro   open-style analog...
3114
3115
3116
  {
  	struct nameidata nd;
  	struct file *file;
669abf4e5   Jeff Layton   vfs: make path_op...
3117
  	struct filename filename = { .name = name };
f9652e10c   Al Viro   allow build_open_...
3118
  	int flags = op->lookup_flags | LOOKUP_ROOT;
73d049a40   Al Viro   open-style analog...
3119
3120
3121
  
  	nd.root.mnt = mnt;
  	nd.root.dentry = dentry;
b18825a7c   David Howells   VFS: Put a small ...
3122
  	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
73d049a40   Al Viro   open-style analog...
3123
  		return ERR_PTR(-ELOOP);
669abf4e5   Jeff Layton   vfs: make path_op...
3124
  	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
73d049a40   Al Viro   open-style analog...
3125
  	if (unlikely(file == ERR_PTR(-ECHILD)))
669abf4e5   Jeff Layton   vfs: make path_op...
3126
  		file = path_openat(-1, &filename, &nd, op, flags);
73d049a40   Al Viro   open-style analog...
3127
  	if (unlikely(file == ERR_PTR(-ESTALE)))
669abf4e5   Jeff Layton   vfs: make path_op...
3128
  		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
73d049a40   Al Viro   open-style analog...
3129
3130
  	return file;
  }
1ac12b4b6   Jeff Layton   vfs: turn is_dir ...
3131
3132
  struct dentry *kern_path_create(int dfd, const char *pathname,
  				struct path *path, unsigned int lookup_flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3133
  {
c663e5d80   Christoph Hellwig   [PATCH] add some ...
3134
  	struct dentry *dentry = ERR_PTR(-EEXIST);
ed75e95de   Al Viro   kill lookup_create()
3135
  	struct nameidata nd;
c30dabfe5   Jan Kara   fs: Push mnt_want...
3136
  	int err2;
1ac12b4b6   Jeff Layton   vfs: turn is_dir ...
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
  	int error;
  	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
  
  	/*
  	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
  	 * other flags passed in are ignored!
  	 */
  	lookup_flags &= LOOKUP_REVAL;
  
  	error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
ed75e95de   Al Viro   kill lookup_create()
3147
3148
  	if (error)
  		return ERR_PTR(error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3149

c663e5d80   Christoph Hellwig   [PATCH] add some ...
3150
3151
3152
3153
  	/*
  	 * Yucky last component or no last component at all?
  	 * (foo/., foo/.., /////)
  	 */
ed75e95de   Al Viro   kill lookup_create()
3154
3155
3156
3157
  	if (nd.last_type != LAST_NORM)
  		goto out;
  	nd.flags &= ~LOOKUP_PARENT;
  	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
3158

c30dabfe5   Jan Kara   fs: Push mnt_want...
3159
3160
  	/* don't fail immediately if it's r/o, at least try to report other errors */
  	err2 = mnt_want_write(nd.path.mnt);
c663e5d80   Christoph Hellwig   [PATCH] add some ...
3161
3162
3163
  	/*
  	 * Do the final lookup.
  	 */
ed75e95de   Al Viro   kill lookup_create()
3164
3165
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  	dentry = lookup_hash(&nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3166
  	if (IS_ERR(dentry))
a8104a9fc   Al Viro   pull mnt_want_wri...
3167
  		goto unlock;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
3168

a8104a9fc   Al Viro   pull mnt_want_wri...
3169
  	error = -EEXIST;
b18825a7c   David Howells   VFS: Put a small ...
3170
  	if (d_is_positive(dentry))
a8104a9fc   Al Viro   pull mnt_want_wri...
3171
  		goto fail;
b18825a7c   David Howells   VFS: Put a small ...
3172

c663e5d80   Christoph Hellwig   [PATCH] add some ...
3173
3174
3175
3176
3177
3178
  	/*
  	 * Special case - lookup gave negative, but... we had foo/bar/
  	 * From the vfs_mknod() POV we just have a negative dentry -
  	 * all is fine. Let's be bastards - you had / on the end, you've
  	 * been asking for (non-existent) directory. -ENOENT for you.
  	 */
ed75e95de   Al Viro   kill lookup_create()
3179
  	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
a8104a9fc   Al Viro   pull mnt_want_wri...
3180
  		error = -ENOENT;
ed75e95de   Al Viro   kill lookup_create()
3181
  		goto fail;
e9baf6e59   Al Viro   [PATCH] return to...
3182
  	}
c30dabfe5   Jan Kara   fs: Push mnt_want...
3183
3184
  	if (unlikely(err2)) {
  		error = err2;
a8104a9fc   Al Viro   pull mnt_want_wri...
3185
  		goto fail;
c30dabfe5   Jan Kara   fs: Push mnt_want...
3186
  	}
ed75e95de   Al Viro   kill lookup_create()
3187
  	*path = nd.path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3188
  	return dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3189
  fail:
a8104a9fc   Al Viro   pull mnt_want_wri...
3190
3191
3192
  	dput(dentry);
  	dentry = ERR_PTR(error);
  unlock:
ed75e95de   Al Viro   kill lookup_create()
3193
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
c30dabfe5   Jan Kara   fs: Push mnt_want...
3194
3195
  	if (!err2)
  		mnt_drop_write(nd.path.mnt);
ed75e95de   Al Viro   kill lookup_create()
3196
3197
  out:
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3198
3199
  	return dentry;
  }
dae6ad8f3   Al Viro   new helpers: kern...
3200
  EXPORT_SYMBOL(kern_path_create);
921a1650d   Al Viro   new helper: done_...
3201
3202
3203
3204
  void done_path_create(struct path *path, struct dentry *dentry)
  {
  	dput(dentry);
  	mutex_unlock(&path->dentry->d_inode->i_mutex);
a8104a9fc   Al Viro   pull mnt_want_wri...
3205
  	mnt_drop_write(path->mnt);
921a1650d   Al Viro   new helper: done_...
3206
3207
3208
  	path_put(path);
  }
  EXPORT_SYMBOL(done_path_create);
1ac12b4b6   Jeff Layton   vfs: turn is_dir ...
3209
3210
  struct dentry *user_path_create(int dfd, const char __user *pathname,
  				struct path *path, unsigned int lookup_flags)
dae6ad8f3   Al Viro   new helpers: kern...
3211
  {
91a27b2a7   Jeff Layton   vfs: define struc...
3212
  	struct filename *tmp = getname(pathname);
dae6ad8f3   Al Viro   new helpers: kern...
3213
3214
3215
  	struct dentry *res;
  	if (IS_ERR(tmp))
  		return ERR_CAST(tmp);
1ac12b4b6   Jeff Layton   vfs: turn is_dir ...
3216
  	res = kern_path_create(dfd, tmp->name, path, lookup_flags);
dae6ad8f3   Al Viro   new helpers: kern...
3217
3218
3219
3220
  	putname(tmp);
  	return res;
  }
  EXPORT_SYMBOL(user_path_create);
1a67aafb5   Al Viro   switch ->mknod() ...
3221
  int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3222
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3223
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3224
3225
3226
  
  	if (error)
  		return error;
975d6b393   Eric W. Biederman   vfs: Don't allow ...
3227
  	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3228
  		return -EPERM;
acfa4380e   Al Viro   inode->i_op is ne...
3229
  	if (!dir->i_op->mknod)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3230
  		return -EPERM;
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
3231
3232
3233
  	error = devcgroup_inode_mknod(mode, dev);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3234
3235
3236
  	error = security_inode_mknod(dir, dentry, mode, dev);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3237
  	error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
3238
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
3239
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3240
3241
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3242
  EXPORT_SYMBOL(vfs_mknod);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3243

f69aac000   Al Viro   switch may_mknod(...
3244
  static int may_mknod(umode_t mode)
463c31972   Dave Hansen   [PATCH] r/o bind ...
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
  {
  	switch (mode & S_IFMT) {
  	case S_IFREG:
  	case S_IFCHR:
  	case S_IFBLK:
  	case S_IFIFO:
  	case S_IFSOCK:
  	case 0: /* zero mode translates to S_IFREG */
  		return 0;
  	case S_IFDIR:
  		return -EPERM;
  	default:
  		return -EINVAL;
  	}
  }
8208a22bb   Al Viro   switch sys_mknoda...
3260
  SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3261
  		unsigned, dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3262
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3263
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3264
3265
  	struct path path;
  	int error;
972567f14   Jeff Layton   vfs: fix mknodat ...
3266
  	unsigned int lookup_flags = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3267

8e4bfca1d   Al Viro   mknod: take sanit...
3268
3269
3270
  	error = may_mknod(mode);
  	if (error)
  		return error;
972567f14   Jeff Layton   vfs: fix mknodat ...
3271
3272
  retry:
  	dentry = user_path_create(dfd, filename, &path, lookup_flags);
dae6ad8f3   Al Viro   new helpers: kern...
3273
3274
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
2ad94ae65   Al Viro   [PATCH] new (loca...
3275

dae6ad8f3   Al Viro   new helpers: kern...
3276
  	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5   Al Viro   New helper - curr...
3277
  		mode &= ~current_umask();
dae6ad8f3   Al Viro   new helpers: kern...
3278
  	error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56a   Kentaro Takeda   introduce new LSM...
3279
  	if (error)
a8104a9fc   Al Viro   pull mnt_want_wri...
3280
  		goto out;
463c31972   Dave Hansen   [PATCH] r/o bind ...
3281
  	switch (mode & S_IFMT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3282
  		case 0: case S_IFREG:
312b63fba   Al Viro   don't pass nameid...
3283
  			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3284
3285
  			break;
  		case S_IFCHR: case S_IFBLK:
dae6ad8f3   Al Viro   new helpers: kern...
3286
  			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3287
3288
3289
  					new_decode_dev(dev));
  			break;
  		case S_IFIFO: case S_IFSOCK:
dae6ad8f3   Al Viro   new helpers: kern...
3290
  			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3291
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3292
  	}
a8104a9fc   Al Viro   pull mnt_want_wri...
3293
  out:
921a1650d   Al Viro   new helper: done_...
3294
  	done_path_create(&path, dentry);
972567f14   Jeff Layton   vfs: fix mknodat ...
3295
3296
3297
3298
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3299
3300
  	return error;
  }
8208a22bb   Al Viro   switch sys_mknoda...
3301
  SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3302
3303
3304
  {
  	return sys_mknodat(AT_FDCWD, filename, mode, dev);
  }
18bb1db3e   Al Viro   switch vfs_mkdir(...
3305
  int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3306
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3307
  	int error = may_create(dir, dentry);
8de527787   Al Viro   vfs: check i_nlin...
3308
  	unsigned max_links = dir->i_sb->s_max_links;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3309
3310
3311
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3312
  	if (!dir->i_op->mkdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3313
3314
3315
3316
3317
3318
  		return -EPERM;
  
  	mode &= (S_IRWXUGO|S_ISVTX);
  	error = security_inode_mkdir(dir, dentry, mode);
  	if (error)
  		return error;
8de527787   Al Viro   vfs: check i_nlin...
3319
3320
  	if (max_links && dir->i_nlink >= max_links)
  		return -EMLINK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3321
  	error = dir->i_op->mkdir(dir, dentry, mode);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
3322
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
3323
  		fsnotify_mkdir(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3324
3325
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3326
  EXPORT_SYMBOL(vfs_mkdir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3327

a218d0fdc   Al Viro   switch open and m...
3328
  SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3329
  {
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3330
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3331
3332
  	struct path path;
  	int error;
b76d8b822   Jeff Layton   vfs: fix mkdirat ...
3333
  	unsigned int lookup_flags = LOOKUP_DIRECTORY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3334

b76d8b822   Jeff Layton   vfs: fix mkdirat ...
3335
3336
  retry:
  	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3337
  	if (IS_ERR(dentry))
dae6ad8f3   Al Viro   new helpers: kern...
3338
  		return PTR_ERR(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3339

dae6ad8f3   Al Viro   new helpers: kern...
3340
  	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5   Al Viro   New helper - curr...
3341
  		mode &= ~current_umask();
dae6ad8f3   Al Viro   new helpers: kern...
3342
  	error = security_path_mkdir(&path, dentry, mode);
a8104a9fc   Al Viro   pull mnt_want_wri...
3343
3344
  	if (!error)
  		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
921a1650d   Al Viro   new helper: done_...
3345
  	done_path_create(&path, dentry);
b76d8b822   Jeff Layton   vfs: fix mkdirat ...
3346
3347
3348
3349
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3350
3351
  	return error;
  }
a218d0fdc   Al Viro   switch open and m...
3352
  SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3353
3354
3355
  {
  	return sys_mkdirat(AT_FDCWD, pathname, mode);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3356
  /*
a71905f0d   Sage Weil   vfs: update dentr...
3357
   * The dentry_unhash() helper will try to drop the dentry early: we
c0d025948   J. Bruce Fields   vfs: fix out-of-d...
3358
   * should have a usage count of 1 if we're the only user of this
a71905f0d   Sage Weil   vfs: update dentr...
3359
3360
   * dentry, and if that is true (possibly after pruning the dcache),
   * then we drop the dentry now.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
   *
   * A low-level filesystem can, if it choses, legally
   * do a
   *
   *	if (!d_unhashed(dentry))
   *		return -EBUSY;
   *
   * if it cannot handle the case of removing a directory
   * that is still in use by something else..
   */
  void dentry_unhash(struct dentry *dentry)
  {
dc168427e   Vasily Averin   [PATCH] VFS: extr...
3373
  	shrink_dcache_parent(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3374
  	spin_lock(&dentry->d_lock);
98474236f   Waiman Long   vfs: make the den...
3375
  	if (dentry->d_lockref.count == 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3376
3377
  		__d_drop(dentry);
  	spin_unlock(&dentry->d_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3378
  }
4d3595073   Al Viro   namei.c: move EXP...
3379
  EXPORT_SYMBOL(dentry_unhash);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3380
3381
3382
3383
3384
3385
3386
  
  int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  {
  	int error = may_delete(dir, dentry, 1);
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3387
  	if (!dir->i_op->rmdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3388
  		return -EPERM;
1d2ef5901   Al Viro   restore pinning t...
3389
  	dget(dentry);
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3390
  	mutex_lock(&dentry->d_inode->i_mutex);
912dbc15d   Sage Weil   vfs: clean up vfs...
3391
3392
  
  	error = -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3393
  	if (d_mountpoint(dentry))
912dbc15d   Sage Weil   vfs: clean up vfs...
3394
3395
3396
3397
3398
  		goto out;
  
  	error = security_inode_rmdir(dir, dentry);
  	if (error)
  		goto out;
3cebde241   Sage Weil   vfs: shrink_dcach...
3399
  	shrink_dcache_parent(dentry);
912dbc15d   Sage Weil   vfs: clean up vfs...
3400
3401
3402
3403
3404
3405
3406
3407
  	error = dir->i_op->rmdir(dir, dentry);
  	if (error)
  		goto out;
  
  	dentry->d_inode->i_flags |= S_DEAD;
  	dont_mount(dentry);
  
  out:
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3408
  	mutex_unlock(&dentry->d_inode->i_mutex);
1d2ef5901   Al Viro   restore pinning t...
3409
  	dput(dentry);
912dbc15d   Sage Weil   vfs: clean up vfs...
3410
  	if (!error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3411
  		d_delete(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3412
3413
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3414
  EXPORT_SYMBOL(vfs_rmdir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3415

5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3416
  static long do_rmdir(int dfd, const char __user *pathname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3417
3418
  {
  	int error = 0;
91a27b2a7   Jeff Layton   vfs: define struc...
3419
  	struct filename *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3420
3421
  	struct dentry *dentry;
  	struct nameidata nd;
c6ee92069   Jeff Layton   vfs: make do_rmdi...
3422
3423
3424
  	unsigned int lookup_flags = 0;
  retry:
  	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
3425
3426
  	if (IS_ERR(name))
  		return PTR_ERR(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3427
3428
  
  	switch(nd.last_type) {
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3429
3430
3431
3432
3433
3434
3435
3436
3437
  	case LAST_DOTDOT:
  		error = -ENOTEMPTY;
  		goto exit1;
  	case LAST_DOT:
  		error = -EINVAL;
  		goto exit1;
  	case LAST_ROOT:
  		error = -EBUSY;
  		goto exit1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3438
  	}
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3439
3440
  
  	nd.flags &= ~LOOKUP_PARENT;
c30dabfe5   Jan Kara   fs: Push mnt_want...
3441
3442
3443
  	error = mnt_want_write(nd.path.mnt);
  	if (error)
  		goto exit1;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3444

4ac913785   Jan Blunck   Embed a struct pa...
3445
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
3446
  	dentry = lookup_hash(&nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3447
  	error = PTR_ERR(dentry);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3448
3449
  	if (IS_ERR(dentry))
  		goto exit2;
e6bc45d65   Theodore Ts'o   vfs: make unlink(...
3450
3451
3452
3453
  	if (!dentry->d_inode) {
  		error = -ENOENT;
  		goto exit3;
  	}
be6d3e56a   Kentaro Takeda   introduce new LSM...
3454
3455
  	error = security_path_rmdir(&nd.path, dentry);
  	if (error)
c30dabfe5   Jan Kara   fs: Push mnt_want...
3456
  		goto exit3;
4ac913785   Jan Blunck   Embed a struct pa...
3457
  	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
0622753b8   Dave Hansen   [PATCH] r/o bind ...
3458
  exit3:
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3459
3460
  	dput(dentry);
  exit2:
4ac913785   Jan Blunck   Embed a struct pa...
3461
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
c30dabfe5   Jan Kara   fs: Push mnt_want...
3462
  	mnt_drop_write(nd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3463
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
3464
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3465
  	putname(name);
c6ee92069   Jeff Layton   vfs: make do_rmdi...
3466
3467
3468
3469
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3470
3471
  	return error;
  }
3cdad4288   Heiko Carstens   [CVE-2009-0029] S...
3472
  SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3473
3474
3475
  {
  	return do_rmdir(AT_FDCWD, pathname);
  }
b21996e36   J. Bruce Fields   locks: break dele...
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
  /**
   * vfs_unlink - unlink a filesystem object
   * @dir:	parent directory
   * @dentry:	victim
   * @delegated_inode: returns victim inode, if the inode is delegated.
   *
   * The caller must hold dir->i_mutex.
   *
   * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
   * return a reference to the inode in delegated_inode.  The caller
   * should then break the delegation on that inode and retry.  Because
   * breaking a delegation may take a long time, the caller should drop
   * dir->i_mutex before doing so.
   *
   * Alternatively, a caller may pass NULL for delegated_inode.  This may
   * be appropriate for callers that expect the underlying filesystem not
   * to be NFS exported.
   */
  int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3495
  {
9accbb977   J. Bruce Fields   namei: minor vfs_...
3496
  	struct inode *target = dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3497
3498
3499
3500
  	int error = may_delete(dir, dentry, 0);
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3501
  	if (!dir->i_op->unlink)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3502
  		return -EPERM;
9accbb977   J. Bruce Fields   namei: minor vfs_...
3503
  	mutex_lock(&target->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3504
3505
3506
3507
  	if (d_mountpoint(dentry))
  		error = -EBUSY;
  	else {
  		error = security_inode_unlink(dir, dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3508
  		if (!error) {
5a14696c1   J. Bruce Fields   locks: helper fun...
3509
3510
  			error = try_break_deleg(target, delegated_inode);
  			if (error)
b21996e36   J. Bruce Fields   locks: break dele...
3511
  				goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3512
  			error = dir->i_op->unlink(dir, dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3513
  			if (!error)
d83c49f3e   Al Viro   Fix the regressio...
3514
  				dont_mount(dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3515
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3516
  	}
b21996e36   J. Bruce Fields   locks: break dele...
3517
  out:
9accbb977   J. Bruce Fields   namei: minor vfs_...
3518
  	mutex_unlock(&target->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3519
3520
3521
  
  	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
  	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
9accbb977   J. Bruce Fields   namei: minor vfs_...
3522
  		fsnotify_link_count(target);
e234f35c5   John McCutchan   [PATCH] inotify d...
3523
  		d_delete(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3524
  	}
0eeca2830   Robert Love   [PATCH] inotify
3525

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3526
3527
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3528
  EXPORT_SYMBOL(vfs_unlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3529
3530
3531
  
  /*
   * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3532
   * directory's i_mutex.  Truncate can take a long time if there is a lot of
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3533
3534
3535
   * writeout happening, and we don't want to prevent access to the directory
   * while waiting on the I/O.
   */
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3536
  static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3537
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3538
  	int error;
91a27b2a7   Jeff Layton   vfs: define struc...
3539
  	struct filename *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3540
3541
3542
  	struct dentry *dentry;
  	struct nameidata nd;
  	struct inode *inode = NULL;
b21996e36   J. Bruce Fields   locks: break dele...
3543
  	struct inode *delegated_inode = NULL;
5d18f8133   Jeff Layton   vfs: make do_unli...
3544
3545
3546
  	unsigned int lookup_flags = 0;
  retry:
  	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
3547
3548
  	if (IS_ERR(name))
  		return PTR_ERR(name);
2ad94ae65   Al Viro   [PATCH] new (loca...
3549

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3550
3551
3552
  	error = -EISDIR;
  	if (nd.last_type != LAST_NORM)
  		goto exit1;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3553
3554
  
  	nd.flags &= ~LOOKUP_PARENT;
c30dabfe5   Jan Kara   fs: Push mnt_want...
3555
3556
3557
  	error = mnt_want_write(nd.path.mnt);
  	if (error)
  		goto exit1;
b21996e36   J. Bruce Fields   locks: break dele...
3558
  retry_deleg:
4ac913785   Jan Blunck   Embed a struct pa...
3559
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
3560
  	dentry = lookup_hash(&nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3561
3562
3563
  	error = PTR_ERR(dentry);
  	if (!IS_ERR(dentry)) {
  		/* Why not before? Because we want correct error value */
50338b889   Török Edwin   fix wrong iput on...
3564
3565
  		if (nd.last.name[nd.last.len])
  			goto slashes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3566
  		inode = dentry->d_inode;
b18825a7c   David Howells   VFS: Put a small ...
3567
  		if (d_is_negative(dentry))
e6bc45d65   Theodore Ts'o   vfs: make unlink(...
3568
3569
  			goto slashes;
  		ihold(inode);
be6d3e56a   Kentaro Takeda   introduce new LSM...
3570
3571
  		error = security_path_unlink(&nd.path, dentry);
  		if (error)
c30dabfe5   Jan Kara   fs: Push mnt_want...
3572
  			goto exit2;
b21996e36   J. Bruce Fields   locks: break dele...
3573
  		error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
c30dabfe5   Jan Kara   fs: Push mnt_want...
3574
  exit2:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3575
3576
  		dput(dentry);
  	}
4ac913785   Jan Blunck   Embed a struct pa...
3577
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3578
3579
  	if (inode)
  		iput(inode);	/* truncate the inode here */
b21996e36   J. Bruce Fields   locks: break dele...
3580
3581
  	inode = NULL;
  	if (delegated_inode) {
5a14696c1   J. Bruce Fields   locks: helper fun...
3582
  		error = break_deleg_wait(&delegated_inode);
b21996e36   J. Bruce Fields   locks: break dele...
3583
3584
3585
  		if (!error)
  			goto retry_deleg;
  	}
c30dabfe5   Jan Kara   fs: Push mnt_want...
3586
  	mnt_drop_write(nd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3587
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
3588
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3589
  	putname(name);
5d18f8133   Jeff Layton   vfs: make do_unli...
3590
3591
3592
3593
3594
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		inode = NULL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3595
3596
3597
  	return error;
  
  slashes:
b18825a7c   David Howells   VFS: Put a small ...
3598
3599
  	if (d_is_negative(dentry))
  		error = -ENOENT;
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
3600
  	else if (d_is_dir(dentry))
b18825a7c   David Howells   VFS: Put a small ...
3601
3602
3603
  		error = -EISDIR;
  	else
  		error = -ENOTDIR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3604
3605
  	goto exit2;
  }
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3606
  SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3607
3608
3609
3610
3611
3612
3613
3614
3615
  {
  	if ((flag & ~AT_REMOVEDIR) != 0)
  		return -EINVAL;
  
  	if (flag & AT_REMOVEDIR)
  		return do_rmdir(dfd, pathname);
  
  	return do_unlinkat(dfd, pathname);
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
3616
  SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3617
3618
3619
  {
  	return do_unlinkat(AT_FDCWD, pathname);
  }
db2e747b1   Miklos Szeredi   [patch 5/5] vfs: ...
3620
  int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3621
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3622
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3623
3624
3625
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3626
  	if (!dir->i_op->symlink)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3627
3628
3629
3630
3631
  		return -EPERM;
  
  	error = security_inode_symlink(dir, dentry, oldname);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3632
  	error = dir->i_op->symlink(dir, dentry, oldname);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
3633
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
3634
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3635
3636
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3637
  EXPORT_SYMBOL(vfs_symlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3638

2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3639
3640
  SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  		int, newdfd, const char __user *, newname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3641
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3642
  	int error;
91a27b2a7   Jeff Layton   vfs: define struc...
3643
  	struct filename *from;
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3644
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3645
  	struct path path;
f46d3567b   Jeff Layton   vfs: fix symlinka...
3646
  	unsigned int lookup_flags = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3647
3648
  
  	from = getname(oldname);
2ad94ae65   Al Viro   [PATCH] new (loca...
3649
  	if (IS_ERR(from))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3650
  		return PTR_ERR(from);
f46d3567b   Jeff Layton   vfs: fix symlinka...
3651
3652
  retry:
  	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3653
3654
  	error = PTR_ERR(dentry);
  	if (IS_ERR(dentry))
dae6ad8f3   Al Viro   new helpers: kern...
3655
  		goto out_putname;
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3656

91a27b2a7   Jeff Layton   vfs: define struc...
3657
  	error = security_path_symlink(&path, dentry, from->name);
a8104a9fc   Al Viro   pull mnt_want_wri...
3658
  	if (!error)
91a27b2a7   Jeff Layton   vfs: define struc...
3659
  		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
921a1650d   Al Viro   new helper: done_...
3660
  	done_path_create(&path, dentry);
f46d3567b   Jeff Layton   vfs: fix symlinka...
3661
3662
3663
3664
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3665
  out_putname:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3666
3667
3668
  	putname(from);
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
3669
  SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3670
3671
3672
  {
  	return sys_symlinkat(oldname, AT_FDCWD, newname);
  }
146a8595c   J. Bruce Fields   locks: break dele...
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
  /**
   * vfs_link - create a new link
   * @old_dentry:	object to be linked
   * @dir:	new parent
   * @new_dentry:	where to create the new link
   * @delegated_inode: returns inode needing a delegation break
   *
   * The caller must hold dir->i_mutex
   *
   * If vfs_link discovers a delegation on the to-be-linked file in need
   * of breaking, it will return -EWOULDBLOCK and return a reference to the
   * inode in delegated_inode.  The caller should then break the delegation
   * and retry.  Because breaking a delegation may take a long time, the
   * caller should drop the i_mutex before doing so.
   *
   * Alternatively, a caller may pass NULL for delegated_inode.  This may
   * be appropriate for callers that expect the underlying filesystem not
   * to be NFS exported.
   */
  int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3693
3694
  {
  	struct inode *inode = old_dentry->d_inode;
8de527787   Al Viro   vfs: check i_nlin...
3695
  	unsigned max_links = dir->i_sb->s_max_links;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3696
3697
3698
3699
  	int error;
  
  	if (!inode)
  		return -ENOENT;
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3700
  	error = may_create(dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
  	if (error)
  		return error;
  
  	if (dir->i_sb != inode->i_sb)
  		return -EXDEV;
  
  	/*
  	 * A link to an append-only or immutable file cannot be created.
  	 */
  	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  		return -EPERM;
acfa4380e   Al Viro   inode->i_op is ne...
3712
  	if (!dir->i_op->link)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3713
  		return -EPERM;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3714
  	if (S_ISDIR(inode->i_mode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3715
3716
3717
3718
3719
  		return -EPERM;
  
  	error = security_inode_link(old_dentry, dir, new_dentry);
  	if (error)
  		return error;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3720
  	mutex_lock(&inode->i_mutex);
aae8a97d3   Aneesh Kumar K.V   fs: Don't allow t...
3721
  	/* Make sure we don't allow creating hardlink to an unlinked file */
f4e0c30c1   Al Viro   allow the temp fi...
3722
  	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
aae8a97d3   Aneesh Kumar K.V   fs: Don't allow t...
3723
  		error =  -ENOENT;
8de527787   Al Viro   vfs: check i_nlin...
3724
3725
  	else if (max_links && inode->i_nlink >= max_links)
  		error = -EMLINK;
146a8595c   J. Bruce Fields   locks: break dele...
3726
3727
3728
3729
3730
  	else {
  		error = try_break_deleg(inode, delegated_inode);
  		if (!error)
  			error = dir->i_op->link(old_dentry, dir, new_dentry);
  	}
f4e0c30c1   Al Viro   allow the temp fi...
3731
3732
3733
3734
3735
3736
  
  	if (!error && (inode->i_state & I_LINKABLE)) {
  		spin_lock(&inode->i_lock);
  		inode->i_state &= ~I_LINKABLE;
  		spin_unlock(&inode->i_lock);
  	}
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3737
  	mutex_unlock(&inode->i_mutex);
e31e14ec3   Stephen Smalley   [PATCH] remove th...
3738
  	if (!error)
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3739
  		fsnotify_link(dir, inode, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3740
3741
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3742
  EXPORT_SYMBOL(vfs_link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
  
  /*
   * Hardlinks are often used in delicate situations.  We avoid
   * security-related surprises by not following symlinks on the
   * newname.  --KAB
   *
   * We don't follow them on the oldname either to be compatible
   * with linux 2.0, and to avoid hard-linking to directories
   * and other special files.  --ADM
   */
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3753
3754
  SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  		int, newdfd, const char __user *, newname, int, flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3755
3756
  {
  	struct dentry *new_dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3757
  	struct path old_path, new_path;
146a8595c   J. Bruce Fields   locks: break dele...
3758
  	struct inode *delegated_inode = NULL;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3759
  	int how = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3760
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3761

11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3762
  	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e16   Ulrich Drepper   [PATCH] flags par...
3763
  		return -EINVAL;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3764
  	/*
f0cc6ffb8   Linus Torvalds   Revert "fs: Allow...
3765
3766
3767
  	 * To use null names we require CAP_DAC_READ_SEARCH
  	 * This ensures that not everyone will be able to create
  	 * handlink using the passed filedescriptor.
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3768
  	 */
f0cc6ffb8   Linus Torvalds   Revert "fs: Allow...
3769
3770
3771
  	if (flags & AT_EMPTY_PATH) {
  		if (!capable(CAP_DAC_READ_SEARCH))
  			return -ENOENT;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3772
  		how = LOOKUP_EMPTY;
f0cc6ffb8   Linus Torvalds   Revert "fs: Allow...
3773
  	}
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3774
3775
3776
  
  	if (flags & AT_SYMLINK_FOLLOW)
  		how |= LOOKUP_FOLLOW;
442e31ca5   Jeff Layton   vfs: fix linkat t...
3777
  retry:
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3778
  	error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3779
  	if (error)
2ad94ae65   Al Viro   [PATCH] new (loca...
3780
  		return error;
442e31ca5   Jeff Layton   vfs: fix linkat t...
3781
3782
  	new_dentry = user_path_create(newdfd, newname, &new_path,
  					(how & LOOKUP_REVAL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3783
  	error = PTR_ERR(new_dentry);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3784
  	if (IS_ERR(new_dentry))
dae6ad8f3   Al Viro   new helpers: kern...
3785
3786
3787
3788
3789
  		goto out;
  
  	error = -EXDEV;
  	if (old_path.mnt != new_path.mnt)
  		goto out_dput;
800179c9b   Kees Cook   fs: add link rest...
3790
3791
3792
  	error = may_linkat(&old_path);
  	if (unlikely(error))
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
3793
  	error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56a   Kentaro Takeda   introduce new LSM...
3794
  	if (error)
a8104a9fc   Al Viro   pull mnt_want_wri...
3795
  		goto out_dput;
146a8595c   J. Bruce Fields   locks: break dele...
3796
  	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
3797
  out_dput:
921a1650d   Al Viro   new helper: done_...
3798
  	done_path_create(&new_path, new_dentry);
146a8595c   J. Bruce Fields   locks: break dele...
3799
3800
  	if (delegated_inode) {
  		error = break_deleg_wait(&delegated_inode);
d22e6338d   Oleg Drokin   Fix mountpoint re...
3801
3802
  		if (!error) {
  			path_put(&old_path);
146a8595c   J. Bruce Fields   locks: break dele...
3803
  			goto retry;
d22e6338d   Oleg Drokin   Fix mountpoint re...
3804
  		}
146a8595c   J. Bruce Fields   locks: break dele...
3805
  	}
442e31ca5   Jeff Layton   vfs: fix linkat t...
3806
  	if (retry_estale(error, how)) {
d22e6338d   Oleg Drokin   Fix mountpoint re...
3807
  		path_put(&old_path);
442e31ca5   Jeff Layton   vfs: fix linkat t...
3808
3809
3810
  		how |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3811
  out:
2d8f30380   Al Viro   [PATCH] sanitize ...
3812
  	path_put(&old_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3813
3814
3815
  
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
3816
  SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3817
  {
c04030e16   Ulrich Drepper   [PATCH] flags par...
3818
  	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3819
  }
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3820
3821
3822
3823
3824
3825
3826
  /**
   * vfs_rename - rename a filesystem object
   * @old_dir:	parent of source
   * @old_dentry:	source
   * @new_dir:	parent of destination
   * @new_dentry:	destination
   * @delegated_inode: returns an inode needing a delegation break
520c8b165   Miklos Szeredi   vfs: add renameat...
3827
   * @flags:	rename flags
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
   *
   * The caller must hold multiple mutexes--see lock_rename()).
   *
   * If vfs_rename discovers a delegation in need of breaking at either
   * the source or destination, it will return -EWOULDBLOCK and return a
   * reference to the inode in delegated_inode.  The caller should then
   * break the delegation and retry.  Because breaking a delegation may
   * take a long time, the caller should drop all locks before doing
   * so.
   *
   * Alternatively, a caller may pass NULL for delegated_inode.  This may
   * be appropriate for callers that expect the underlying filesystem not
   * to be NFS exported.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3842
3843
3844
3845
3846
3847
   * The worst of all namespace operations - renaming directory. "Perverted"
   * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
   * Problems:
   *	a) we can get into loop creation. Check is done in is_subdir().
   *	b) race potential - two innocent renames can create a loop together.
   *	   That's where 4.4 screws up. Current fix: serialization on
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
3848
   *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3849
   *	   story.
6cedba896   J. Bruce Fields   vfs: take i_mutex...
3850
3851
   *	c) we have to lock _four_ objects - parents and victim (if it exists),
   *	   and source (if it is not a directory).
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3852
   *	   And that - after we got ->i_mutex on parents (until then we don't know
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3853
3854
   *	   whether the target exists).  Solution: try to be smart with locking
   *	   order for inodes.  We rely on the fact that tree topology may change
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
3855
   *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3856
3857
3858
   *	   move will be locked.  Thus we can rank directories by the tree
   *	   (ancestors first) and rank all non-directories after them.
   *	   That works since everybody except rename does "lock parent, lookup,
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
3859
   *	   lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3860
3861
3862
   *	   HOWEVER, it relies on the assumption that any object with ->lookup()
   *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
   *	   we'd better make sure that there's no link(2) for them.
e4eaac06b   Sage Weil   vfs: push dentry_...
3863
   *	d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3864
   *	   we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3865
   *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e72   Adam Buchbinder   Fix misspellings ...
3866
   *	   ->i_mutex on parents, which works but leads to some truly excessive
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3867
3868
   *	   locking].
   */
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3869
3870
  int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  	       struct inode *new_dir, struct dentry *new_dentry,
520c8b165   Miklos Szeredi   vfs: add renameat...
3871
  	       struct inode **delegated_inode, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3872
  {
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3873
3874
3875
3876
  	int error;
  	bool is_dir = d_is_dir(old_dentry);
  	const unsigned char *old_name;
  	struct inode *source = old_dentry->d_inode;
9055cba71   Sage Weil   vfs: clean up vfs...
3877
  	struct inode *target = new_dentry->d_inode;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3878
3879
  	bool new_is_dir = false;
  	unsigned max_links = new_dir->i_sb->s_max_links;
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3880
3881
3882
3883
3884
3885
3886
  
  	if (source == target)
  		return 0;
  
  	error = may_delete(old_dir, old_dentry, is_dir);
  	if (error)
  		return error;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3887
  	if (!target) {
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3888
  		error = may_create(new_dir, new_dentry);
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3889
3890
3891
3892
3893
3894
3895
3896
  	} else {
  		new_is_dir = d_is_dir(new_dentry);
  
  		if (!(flags & RENAME_EXCHANGE))
  			error = may_delete(new_dir, new_dentry, is_dir);
  		else
  			error = may_delete(new_dir, new_dentry, new_is_dir);
  	}
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3897
3898
3899
3900
3901
  	if (error)
  		return error;
  
  	if (!old_dir->i_op->rename)
  		return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3902

520c8b165   Miklos Szeredi   vfs: add renameat...
3903
3904
  	if (flags && !old_dir->i_op->rename2)
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3905
3906
3907
3908
  	/*
  	 * If we are going to change the parent - check write permissions,
  	 * we'll need to flip '..'.
  	 */
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
  	if (new_dir != old_dir) {
  		if (is_dir) {
  			error = inode_permission(source, MAY_WRITE);
  			if (error)
  				return error;
  		}
  		if ((flags & RENAME_EXCHANGE) && new_is_dir) {
  			error = inode_permission(target, MAY_WRITE);
  			if (error)
  				return error;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3920
  	}
0b3974eb0   Miklos Szeredi   security: add fla...
3921
3922
  	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
  				      flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3923
3924
  	if (error)
  		return error;
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3925
  	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
1d2ef5901   Al Viro   restore pinning t...
3926
  	dget(new_dentry);
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3927
  	if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3928
3929
  		lock_two_nondirectories(source, target);
  	else if (target)
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3930
  		mutex_lock(&target->i_mutex);
9055cba71   Sage Weil   vfs: clean up vfs...
3931
3932
3933
3934
  
  	error = -EBUSY;
  	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  		goto out;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3935
  	if (max_links && new_dir != old_dir) {
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3936
  		error = -EMLINK;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3937
  		if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3938
  			goto out;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3939
3940
3941
3942
3943
3944
3945
  		if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
  		    old_dir->i_nlink >= max_links)
  			goto out;
  	}
  	if (is_dir && !(flags & RENAME_EXCHANGE) && target)
  		shrink_dcache_parent(new_dentry);
  	if (!is_dir) {
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3946
  		error = try_break_deleg(source, delegated_inode);
8e6d782ca   J. Bruce Fields   locks: break dele...
3947
3948
  		if (error)
  			goto out;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3949
3950
3951
3952
3953
  	}
  	if (target && !new_is_dir) {
  		error = try_break_deleg(target, delegated_inode);
  		if (error)
  			goto out;
8e6d782ca   J. Bruce Fields   locks: break dele...
3954
  	}
520c8b165   Miklos Szeredi   vfs: add renameat...
3955
3956
3957
3958
3959
3960
3961
  	if (!flags) {
  		error = old_dir->i_op->rename(old_dir, old_dentry,
  					      new_dir, new_dentry);
  	} else {
  		error = old_dir->i_op->rename2(old_dir, old_dentry,
  					       new_dir, new_dentry, flags);
  	}
51892bbb5   Sage Weil   vfs: clean up vfs...
3962
3963
  	if (error)
  		goto out;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3964
  	if (!(flags & RENAME_EXCHANGE) && target) {
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3965
3966
  		if (is_dir)
  			target->i_flags |= S_DEAD;
51892bbb5   Sage Weil   vfs: clean up vfs...
3967
  		dont_mount(new_dentry);
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3968
  	}
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3969
3970
3971
3972
3973
3974
  	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
  		if (!(flags & RENAME_EXCHANGE))
  			d_move(old_dentry, new_dentry);
  		else
  			d_exchange(old_dentry, new_dentry);
  	}
51892bbb5   Sage Weil   vfs: clean up vfs...
3975
  out:
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3976
  	if (!is_dir || (flags & RENAME_EXCHANGE))
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3977
3978
3979
  		unlock_two_nondirectories(source, target);
  	else if (target)
  		mutex_unlock(&target->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3980
  	dput(new_dentry);
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3981
  	if (!error) {
123df2944   Al Viro   Lose the new_name...
3982
  		fsnotify_move(old_dir, new_dir, old_name, is_dir,
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3983
3984
3985
3986
3987
3988
  			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
  		if (flags & RENAME_EXCHANGE) {
  			fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
  				      new_is_dir, NULL, new_dentry);
  		}
  	}
0eeca2830   Robert Love   [PATCH] inotify
3989
  	fsnotify_oldname_free(old_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3990
3991
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3992
  EXPORT_SYMBOL(vfs_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3993

520c8b165   Miklos Szeredi   vfs: add renameat...
3994
3995
  SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
  		int, newdfd, const char __user *, newname, unsigned int, flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3996
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3997
3998
3999
  	struct dentry *old_dir, *new_dir;
  	struct dentry *old_dentry, *new_dentry;
  	struct dentry *trap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4000
  	struct nameidata oldnd, newnd;
8e6d782ca   J. Bruce Fields   locks: break dele...
4001
  	struct inode *delegated_inode = NULL;
91a27b2a7   Jeff Layton   vfs: define struc...
4002
4003
  	struct filename *from;
  	struct filename *to;
c6a942840   Jeff Layton   vfs: fix renameat...
4004
4005
  	unsigned int lookup_flags = 0;
  	bool should_retry = false;
2ad94ae65   Al Viro   [PATCH] new (loca...
4006
  	int error;
520c8b165   Miklos Szeredi   vfs: add renameat...
4007

da1ce0670   Miklos Szeredi   vfs: add cross-re...
4008
4009
4010
4011
  	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  		return -EINVAL;
  
  	if ((flags & RENAME_NOREPLACE) && (flags & RENAME_EXCHANGE))
520c8b165   Miklos Szeredi   vfs: add renameat...
4012
  		return -EINVAL;
c6a942840   Jeff Layton   vfs: fix renameat...
4013
4014
  retry:
  	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
4015
4016
  	if (IS_ERR(from)) {
  		error = PTR_ERR(from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4017
  		goto exit;
91a27b2a7   Jeff Layton   vfs: define struc...
4018
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4019

c6a942840   Jeff Layton   vfs: fix renameat...
4020
  	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
4021
4022
  	if (IS_ERR(to)) {
  		error = PTR_ERR(to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4023
  		goto exit1;
91a27b2a7   Jeff Layton   vfs: define struc...
4024
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4025
4026
  
  	error = -EXDEV;
4ac913785   Jan Blunck   Embed a struct pa...
4027
  	if (oldnd.path.mnt != newnd.path.mnt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4028
  		goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
4029
  	old_dir = oldnd.path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4030
4031
4032
  	error = -EBUSY;
  	if (oldnd.last_type != LAST_NORM)
  		goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
4033
  	new_dir = newnd.path.dentry;
0a7c3937a   Miklos Szeredi   vfs: add RENAME_N...
4034
4035
  	if (flags & RENAME_NOREPLACE)
  		error = -EEXIST;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4036
4037
  	if (newnd.last_type != LAST_NORM)
  		goto exit2;
c30dabfe5   Jan Kara   fs: Push mnt_want...
4038
4039
4040
  	error = mnt_want_write(oldnd.path.mnt);
  	if (error)
  		goto exit2;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
4041
4042
  	oldnd.flags &= ~LOOKUP_PARENT;
  	newnd.flags &= ~LOOKUP_PARENT;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
4043
4044
  	if (!(flags & RENAME_EXCHANGE))
  		newnd.flags |= LOOKUP_RENAME_TARGET;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
4045

8e6d782ca   J. Bruce Fields   locks: break dele...
4046
  retry_deleg:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4047
  	trap = lock_rename(new_dir, old_dir);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
4048
  	old_dentry = lookup_hash(&oldnd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4049
4050
4051
4052
4053
  	error = PTR_ERR(old_dentry);
  	if (IS_ERR(old_dentry))
  		goto exit3;
  	/* source must exist */
  	error = -ENOENT;
b18825a7c   David Howells   VFS: Put a small ...
4054
  	if (d_is_negative(old_dentry))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4055
  		goto exit4;
0a7c3937a   Miklos Szeredi   vfs: add RENAME_N...
4056
4057
4058
4059
4060
4061
4062
  	new_dentry = lookup_hash(&newnd);
  	error = PTR_ERR(new_dentry);
  	if (IS_ERR(new_dentry))
  		goto exit4;
  	error = -EEXIST;
  	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
  		goto exit5;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
  	if (flags & RENAME_EXCHANGE) {
  		error = -ENOENT;
  		if (d_is_negative(new_dentry))
  			goto exit5;
  
  		if (!d_is_dir(new_dentry)) {
  			error = -ENOTDIR;
  			if (newnd.last.name[newnd.last.len])
  				goto exit5;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4074
  	/* unless the source is a directory trailing slashes give -ENOTDIR */
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
4075
  	if (!d_is_dir(old_dentry)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4076
4077
  		error = -ENOTDIR;
  		if (oldnd.last.name[oldnd.last.len])
0a7c3937a   Miklos Szeredi   vfs: add RENAME_N...
4078
  			goto exit5;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
4079
  		if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
0a7c3937a   Miklos Szeredi   vfs: add RENAME_N...
4080
  			goto exit5;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4081
4082
4083
4084
  	}
  	/* source should not be ancestor of target */
  	error = -EINVAL;
  	if (old_dentry == trap)
0a7c3937a   Miklos Szeredi   vfs: add RENAME_N...
4085
  		goto exit5;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4086
  	/* target should not be an ancestor of source */
da1ce0670   Miklos Szeredi   vfs: add cross-re...
4087
4088
  	if (!(flags & RENAME_EXCHANGE))
  		error = -ENOTEMPTY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4089
4090
  	if (new_dentry == trap)
  		goto exit5;
be6d3e56a   Kentaro Takeda   introduce new LSM...
4091
  	error = security_path_rename(&oldnd.path, old_dentry,
0b3974eb0   Miklos Szeredi   security: add fla...
4092
  				     &newnd.path, new_dentry, flags);
be6d3e56a   Kentaro Takeda   introduce new LSM...
4093
  	if (error)
c30dabfe5   Jan Kara   fs: Push mnt_want...
4094
  		goto exit5;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4095
  	error = vfs_rename(old_dir->d_inode, old_dentry,
520c8b165   Miklos Szeredi   vfs: add renameat...
4096
4097
  			   new_dir->d_inode, new_dentry,
  			   &delegated_inode, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4098
4099
4100
4101
4102
4103
  exit5:
  	dput(new_dentry);
  exit4:
  	dput(old_dentry);
  exit3:
  	unlock_rename(new_dir, old_dir);
8e6d782ca   J. Bruce Fields   locks: break dele...
4104
4105
4106
4107
4108
  	if (delegated_inode) {
  		error = break_deleg_wait(&delegated_inode);
  		if (!error)
  			goto retry_deleg;
  	}
c30dabfe5   Jan Kara   fs: Push mnt_want...
4109
  	mnt_drop_write(oldnd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4110
  exit2:
c6a942840   Jeff Layton   vfs: fix renameat...
4111
4112
  	if (retry_estale(error, lookup_flags))
  		should_retry = true;
1d957f9bf   Jan Blunck   Introduce path_put()
4113
  	path_put(&newnd.path);
2ad94ae65   Al Viro   [PATCH] new (loca...
4114
  	putname(to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4115
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
4116
  	path_put(&oldnd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4117
  	putname(from);
c6a942840   Jeff Layton   vfs: fix renameat...
4118
4119
4120
4121
4122
  	if (should_retry) {
  		should_retry = false;
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
2ad94ae65   Al Viro   [PATCH] new (loca...
4123
  exit:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4124
4125
  	return error;
  }
520c8b165   Miklos Szeredi   vfs: add renameat...
4126
4127
4128
4129
4130
  SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  		int, newdfd, const char __user *, newname)
  {
  	return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
  }
a26eab240   Heiko Carstens   [CVE-2009-0029] S...
4131
  SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
4132
  {
520c8b165   Miklos Szeredi   vfs: add renameat...
4133
  	return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
4134
  }
5d826c847   Al Viro   new helper: readl...
4135
  int readlink_copy(char __user *buffer, int buflen, const char *link)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4136
  {
5d826c847   Al Viro   new helper: readl...
4137
  	int len = PTR_ERR(link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
  	if (IS_ERR(link))
  		goto out;
  
  	len = strlen(link);
  	if (len > (unsigned) buflen)
  		len = buflen;
  	if (copy_to_user(buffer, link, len))
  		len = -EFAULT;
  out:
  	return len;
  }
5d826c847   Al Viro   new helper: readl...
4149
  EXPORT_SYMBOL(readlink_copy);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4150
4151
4152
4153
4154
4155
4156
4157
4158
  
  /*
   * A helper for ->readlink().  This should be used *ONLY* for symlinks that
   * have ->follow_link() touching nd only in nd_set_link().  Using (or not
   * using) it for any given inode is up to filesystem.
   */
  int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  {
  	struct nameidata nd;
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4159
  	void *cookie;
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
4160
  	int res;
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4161

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4162
  	nd.depth = 0;
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4163
  	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
4164
4165
  	if (IS_ERR(cookie))
  		return PTR_ERR(cookie);
5d826c847   Al Viro   new helper: readl...
4166
  	res = readlink_copy(buffer, buflen, nd_get_link(&nd));
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
4167
4168
4169
  	if (dentry->d_inode->i_op->put_link)
  		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  	return res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4170
  }
4d3595073   Al Viro   namei.c: move EXP...
4171
  EXPORT_SYMBOL(generic_readlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4172

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4173
4174
4175
  /* get the link contents into pagecache */
  static char *page_getlink(struct dentry * dentry, struct page **ppage)
  {
ebd09abbd   Duane Griffin   vfs: ensure page ...
4176
4177
  	char *kaddr;
  	struct page *page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4178
  	struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b185   Pekka Enberg   [PATCH] read_mapp...
4179
  	page = read_mapping_page(mapping, 0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4180
  	if (IS_ERR(page))
6fe6900e1   Nick Piggin   mm: make read_cac...
4181
  		return (char*)page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4182
  	*ppage = page;
ebd09abbd   Duane Griffin   vfs: ensure page ...
4183
4184
4185
  	kaddr = kmap(page);
  	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  	return kaddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4186
4187
4188
4189
4190
  }
  
  int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  {
  	struct page *page = NULL;
5d826c847   Al Viro   new helper: readl...
4191
  	int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4192
4193
4194
4195
4196
4197
  	if (page) {
  		kunmap(page);
  		page_cache_release(page);
  	}
  	return res;
  }
4d3595073   Al Viro   namei.c: move EXP...
4198
  EXPORT_SYMBOL(page_readlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4199

cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4200
  void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4201
  {
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4202
  	struct page *page = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4203
  	nd_set_link(nd, page_getlink(dentry, &page));
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4204
  	return page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4205
  }
4d3595073   Al Viro   namei.c: move EXP...
4206
  EXPORT_SYMBOL(page_follow_link_light);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4207

cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4208
  void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4209
  {
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4210
4211
4212
  	struct page *page = cookie;
  
  	if (page) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4213
4214
  		kunmap(page);
  		page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4215
4216
  	}
  }
4d3595073   Al Viro   namei.c: move EXP...
4217
  EXPORT_SYMBOL(page_put_link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4218

54566b2c1   Nick Piggin   fs: symlink write...
4219
4220
4221
4222
  /*
   * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
   */
  int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4223
4224
  {
  	struct address_space *mapping = inode->i_mapping;
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
4225
  	struct page *page;
afddba49d   Nick Piggin   fs: introduce wri...
4226
  	void *fsdata;
beb497ab4   Dmitriy Monakhov   [PATCH] __page_sy...
4227
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4228
  	char *kaddr;
54566b2c1   Nick Piggin   fs: symlink write...
4229
4230
4231
  	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  	if (nofs)
  		flags |= AOP_FLAG_NOFS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4232

7e53cac41   NeilBrown   [PATCH] Honour AO...
4233
  retry:
afddba49d   Nick Piggin   fs: introduce wri...
4234
  	err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c1   Nick Piggin   fs: symlink write...
4235
  				flags, &page, &fsdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4236
  	if (err)
afddba49d   Nick Piggin   fs: introduce wri...
4237
  		goto fail;
e8e3c3d66   Cong Wang   fs: remove the se...
4238
  	kaddr = kmap_atomic(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4239
  	memcpy(kaddr, symname, len-1);
e8e3c3d66   Cong Wang   fs: remove the se...
4240
  	kunmap_atomic(kaddr);
afddba49d   Nick Piggin   fs: introduce wri...
4241
4242
4243
  
  	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  							page, fsdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4244
4245
  	if (err < 0)
  		goto fail;
afddba49d   Nick Piggin   fs: introduce wri...
4246
4247
  	if (err < len-1)
  		goto retry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4248
4249
  	mark_inode_dirty(inode);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4250
4251
4252
  fail:
  	return err;
  }
4d3595073   Al Viro   namei.c: move EXP...
4253
  EXPORT_SYMBOL(__page_symlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4254

0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
4255
4256
4257
  int page_symlink(struct inode *inode, const char *symname, int len)
  {
  	return __page_symlink(inode, symname, len,
54566b2c1   Nick Piggin   fs: symlink write...
4258
  			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
4259
  }
4d3595073   Al Viro   namei.c: move EXP...
4260
  EXPORT_SYMBOL(page_symlink);
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
4261

92e1d5be9   Arjan van de Ven   [PATCH] mark stru...
4262
  const struct inode_operations page_symlink_inode_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4263
4264
4265
4266
  	.readlink	= generic_readlink,
  	.follow_link	= page_follow_link_light,
  	.put_link	= page_put_link,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4267
  EXPORT_SYMBOL(page_symlink_inode_operations);