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

3ddcd0569   Linus Torvalds   vfs: optimize ino...
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  /*
   * 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
370
  /**
0bdaea901   David Howells   VFS: Split inode_...
371
372
373
   * __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
374
   *
0bdaea901   David Howells   VFS: Split inode_...
375
   * Check for read/write/execute permissions on an inode.
948409c74   Andreas Gruenbacher   vfs: add a commen...
376
377
   *
   * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
0bdaea901   David Howells   VFS: Split inode_...
378
379
380
   *
   * This does not check for a read-only file system.  You probably want
   * inode_permission().
cb23beb55   Christoph Hellwig   kill vfs_permission
381
   */
0bdaea901   David Howells   VFS: Split inode_...
382
  int __inode_permission(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
  {
e6305c43e   Al Viro   [PATCH] sanitize ...
384
  	int retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385

3ddcd0569   Linus Torvalds   vfs: optimize ino...
386
  	if (unlikely(mask & MAY_WRITE)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
388
389
390
391
392
  		/*
  		 * Nobody gets write access to an immutable file.
  		 */
  		if (IS_IMMUTABLE(inode))
  			return -EACCES;
  	}
3ddcd0569   Linus Torvalds   vfs: optimize ino...
393
  	retval = do_inode_permission(inode, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
395
  	if (retval)
  		return retval;
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
396
397
398
  	retval = devcgroup_inode_permission(inode, mask);
  	if (retval)
  		return retval;
d09ca7397   Eric Paris   security: make LS...
399
  	return security_inode_permission(inode, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
  }
f4d6ff89d   Al Viro   move exec_permiss...
401
  /**
0bdaea901   David Howells   VFS: Split inode_...
402
403
   * sb_permission - Check superblock-level permissions
   * @sb: Superblock of inode to check permission on
55852635a   Randy Dunlap   fs: fix fs/namei....
404
   * @inode: Inode to check permission on
0bdaea901   David Howells   VFS: Split inode_...
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
441
   * @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...
442
  EXPORT_SYMBOL(inode_permission);
0bdaea901   David Howells   VFS: Split inode_...
443
444
  
  /**
5dd784d04   Jan Blunck   Introduce path_get()
445
446
447
448
449
   * 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...
450
  void path_get(const struct path *path)
5dd784d04   Jan Blunck   Introduce path_get()
451
452
453
454
455
456
457
  {
  	mntget(path->mnt);
  	dget(path->dentry);
  }
  EXPORT_SYMBOL(path_get);
  
  /**
1d957f9bf   Jan Blunck   Introduce path_put()
458
459
460
461
462
   * 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...
463
  void path_put(const struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
464
  {
1d957f9bf   Jan Blunck   Introduce path_put()
465
466
  	dput(path->dentry);
  	mntput(path->mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
  }
1d957f9bf   Jan Blunck   Introduce path_put()
468
  EXPORT_SYMBOL(path_put);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469

19660af73   Al Viro   consolidate namei...
470
  /*
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
471
   * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af73   Al Viro   consolidate namei...
472
473
474
475
476
477
478
   * 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 ...
479
   */
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
480
481
  
  /**
19660af73   Al Viro   consolidate namei...
482
483
484
   * 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...
485
   * Returns: 0 on success, -ECHILD on failure
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
486
   *
19660af73   Al Viro   consolidate namei...
487
488
489
   * 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 ...
490
   */
19660af73   Al Viro   consolidate namei...
491
  static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
492
493
494
495
496
  {
  	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...
497
498
  
  	/*
48a066e72   Al Viro   RCU'd vfsmounts
499
500
501
502
503
504
  	 * 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...
505
  	 */
48a066e72   Al Viro   RCU'd vfsmounts
506
  	if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
e5c832d55   Linus Torvalds   vfs: fix dentry R...
507
  		return -ECHILD;
e5c832d55   Linus Torvalds   vfs: fix dentry R...
508
  	nd->flags &= ~LOOKUP_RCU;
15570086b   Linus Torvalds   vfs: reimplement ...
509

48a066e72   Al Viro   RCU'd vfsmounts
510
511
  	if (!lockref_get_not_dead(&parent->d_lockref)) {
  		nd->path.dentry = NULL;	
d870b4a19   Al Viro   fix bogus path_pu...
512
  		goto out;
48a066e72   Al Viro   RCU'd vfsmounts
513
  	}
15570086b   Linus Torvalds   vfs: reimplement ...
514
515
516
517
518
519
520
521
522
523
524
  	/*
  	 * 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...
525
  	if (!dentry) {
e5c832d55   Linus Torvalds   vfs: fix dentry R...
526
527
  		if (read_seqcount_retry(&parent->d_seq, nd->seq))
  			goto out;
19660af73   Al Viro   consolidate namei...
528
529
  		BUG_ON(nd->inode != parent->d_inode);
  	} else {
e5c832d55   Linus Torvalds   vfs: fix dentry R...
530
531
532
533
  		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...
534
  	}
e5c832d55   Linus Torvalds   vfs: fix dentry R...
535
536
537
538
539
540
541
542
543
  
  	/*
  	 * 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 ...
544
545
546
  		path_get(&nd->root);
  		spin_unlock(&fs->lock);
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
547

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

e5c832d55   Linus Torvalds   vfs: fix dentry R...
551
552
553
  unlock_and_drop_dentry:
  	spin_unlock(&fs->lock);
  drop_dentry:
8b61e74ff   Al Viro   get rid of {lock,...
554
  	rcu_read_unlock();
15570086b   Linus Torvalds   vfs: reimplement ...
555
  	dput(dentry);
d0d272771   Linus Torvalds   vfs: make sure we...
556
  	goto drop_root_mnt;
e5c832d55   Linus Torvalds   vfs: fix dentry R...
557
  out:
8b61e74ff   Al Viro   get rid of {lock,...
558
  	rcu_read_unlock();
d0d272771   Linus Torvalds   vfs: make sure we...
559
560
561
  drop_root_mnt:
  	if (!(nd->flags & LOOKUP_ROOT))
  		nd->root.mnt = NULL;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
562
563
  	return -ECHILD;
  }
4ce16ef3f   Al Viro   fs/namei.c: don't...
564
  static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
34286d666   Nick Piggin   fs: rcu-walk awar...
565
  {
4ce16ef3f   Al Viro   fs/namei.c: don't...
566
  	return dentry->d_op->d_revalidate(dentry, flags);
34286d666   Nick Piggin   fs: rcu-walk awar...
567
  }
9f1fafee9   Al Viro   merge handle_reva...
568
569
570
  /**
   * complete_walk - successful completion of path walk
   * @nd:  pointer nameidata
39159de2a   Jeff Layton   vfs: force reval ...
571
   *
9f1fafee9   Al Viro   merge handle_reva...
572
573
574
575
576
   * 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 ...
577
   */
9f1fafee9   Al Viro   merge handle_reva...
578
  static int complete_walk(struct nameidata *nd)
39159de2a   Jeff Layton   vfs: force reval ...
579
  {
16c2cd717   Al Viro   untangle the "nee...
580
  	struct dentry *dentry = nd->path.dentry;
39159de2a   Jeff Layton   vfs: force reval ...
581
  	int status;
39159de2a   Jeff Layton   vfs: force reval ...
582

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

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

9f1fafee9   Al Viro   merge handle_reva...
615
  	path_put(&nd->path);
39159de2a   Jeff Layton   vfs: force reval ...
616
617
  	return status;
  }
2a7378711   Al Viro   Cache root in nam...
618
619
  static __always_inline void set_root(struct nameidata *nd)
  {
f7ad3c6be   Miklos Szeredi   vfs: add helpers ...
620
621
  	if (!nd->root.mnt)
  		get_fs_root(current->fs, &nd->root);
2a7378711   Al Viro   Cache root in nam...
622
  }
6de88d729   Al Viro   kill __link_path_...
623
  static int link_path_walk(const char *, struct nameidata *);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
624
625
626
627
  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...
628
629
630
631
632
  		unsigned seq;
  
  		do {
  			seq = read_seqcount_begin(&fs->seq);
  			nd->root = fs->root;
c1530019e   Tim Chen   vfs: Fix absolute...
633
  			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
c28cc3646   Nick Piggin   fs: fs_struct use...
634
  		} while (read_seqcount_retry(&fs->seq, seq));
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
635
636
  	}
  }
1d957f9bf   Jan Blunck   Introduce path_put()
637
  static void path_put_conditional(struct path *path, struct nameidata *nd)
051d38125   Ian Kent   [PATCH] autofs4: ...
638
639
  {
  	dput(path->dentry);
4ac913785   Jan Blunck   Embed a struct pa...
640
  	if (path->mnt != nd->path.mnt)
051d38125   Ian Kent   [PATCH] autofs4: ...
641
642
  		mntput(path->mnt);
  }
7b9337aaf   Nick Piggin   fs: namei fix ->p...
643
644
  static inline void path_to_nameidata(const struct path *path,
  					struct nameidata *nd)
051d38125   Ian Kent   [PATCH] autofs4: ...
645
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
646
647
648
649
  	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 ...
650
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
651
  	nd->path.mnt = path->mnt;
4ac913785   Jan Blunck   Embed a struct pa...
652
  	nd->path.dentry = path->dentry;
051d38125   Ian Kent   [PATCH] autofs4: ...
653
  }
b5fb63c18   Christoph Hellwig   fs: add nd_jump_link
654
655
656
657
658
659
660
661
662
663
664
  /*
   * 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
665
  }
574197e0d   Al Viro   tidy the trailing...
666
667
668
  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...
669
  	if (inode->i_op->put_link)
574197e0d   Al Viro   tidy the trailing...
670
671
672
  		inode->i_op->put_link(link->dentry, nd, cookie);
  	path_put(link);
  }
561ec64ae   Linus Torvalds   VFS: don't do pro...
673
674
  int sysctl_protected_symlinks __read_mostly = 0;
  int sysctl_protected_hardlinks __read_mostly = 0;
800179c9b   Kees Cook   fs: add link rest...
675
676
677
678
  
  /**
   * may_follow_link - Check symlink following for unsafe situations
   * @link: The path of the symlink
55852635a   Randy Dunlap   fs: fix fs/namei....
679
   * @nd: nameidata pathwalk data
800179c9b   Kees Cook   fs: add link rest...
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
   *
   * 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...
702
  	if (uid_eq(current_cred()->fsuid, inode->i_uid))
800179c9b   Kees Cook   fs: add link rest...
703
704
705
706
707
708
709
710
  		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...
711
  	if (uid_eq(parent->i_uid, inode->i_uid))
800179c9b   Kees Cook   fs: add link rest...
712
  		return 0;
ffd8d101a   Sasha Levin   fs: prevent use a...
713
  	audit_log_link_denied("follow_link", link);
800179c9b   Kees Cook   fs: add link rest...
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
779
  	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...
780
  	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
800179c9b   Kees Cook   fs: add link rest...
781
782
  	    capable(CAP_FOWNER))
  		return 0;
a51d9eaa4   Kees Cook   fs: add link rest...
783
  	audit_log_link_denied("linkat", link);
800179c9b   Kees Cook   fs: add link rest...
784
785
  	return -EPERM;
  }
def4af30c   Al Viro   Get rid of symlin...
786
  static __always_inline int
574197e0d   Al Viro   tidy the trailing...
787
  follow_link(struct path *link, struct nameidata *nd, void **p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
  {
7b9337aaf   Nick Piggin   fs: namei fix ->p...
789
  	struct dentry *dentry = link->dentry;
6d7b5aaed   Al Viro   namei.c: let foll...
790
791
  	int error;
  	char *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792

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

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

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

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

b5c84bf6f   Nick Piggin   fs: dcache remove...
889
  /*
9875cf806   David Howells   Add a dentry op t...
890
891
892
   * 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
893
   */
9875cf806   David Howells   Add a dentry op t...
894
895
  static int follow_automount(struct path *path, unsigned flags,
  			    bool *need_mntput)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
896
  {
9875cf806   David Howells   Add a dentry op t...
897
  	struct vfsmount *mnt;
ea5b778a8   David Howells   Unexport do_add_m...
898
  	int err;
9875cf806   David Howells   Add a dentry op t...
899
900
901
  
  	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  		return -EREMOTE;
0ec26fd06   Miklos Szeredi   vfs: automount sh...
902
903
904
905
906
907
908
909
910
911
  	/* 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...
912
  	 */
0ec26fd06   Miklos Szeredi   vfs: automount sh...
913
  	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
d94c177be   Linus Torvalds   vfs pathname look...
914
  		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
0ec26fd06   Miklos Szeredi   vfs: automount sh...
915
916
  	    path->dentry->d_inode)
  		return -EISDIR;
9875cf806   David Howells   Add a dentry op t...
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
  	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
932
  		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9875cf806   David Howells   Add a dentry op t...
933
934
  			return -EREMOTE;
  		return PTR_ERR(mnt);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
935
  	}
ea5b778a8   David Howells   Unexport do_add_m...
936

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

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

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

463ffb2e9   Al Viro   [PATCH] namei fix...
960
  }
9875cf806   David Howells   Add a dentry op t...
961
962
  /*
   * Handle a dentry that is managed in some way.
cc53ce53c   David Howells   Add a dentry op t...
963
   * - Flagged for transit management (autofs)
9875cf806   David Howells   Add a dentry op t...
964
965
966
967
968
969
970
971
   * - 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
972
  {
8aef18845   Al Viro   VFS: Fix vfsmount...
973
  	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9875cf806   David Howells   Add a dentry op t...
974
975
  	unsigned managed;
  	bool need_mntput = false;
8aef18845   Al Viro   VFS: Fix vfsmount...
976
  	int ret = 0;
9875cf806   David Howells   Add a dentry op t...
977
978
979
980
981
982
983
  
  	/* 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...
984
985
986
987
988
  		/* 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...
989
  			ret = path->dentry->d_op->d_manage(path->dentry, false);
cc53ce53c   David Howells   Add a dentry op t...
990
  			if (ret < 0)
8aef18845   Al Viro   VFS: Fix vfsmount...
991
  				break;
cc53ce53c   David Howells   Add a dentry op t...
992
  		}
9875cf806   David Howells   Add a dentry op t...
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
  		/* 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
1008
1009
  			 * namespace got unmounted before lookup_mnt() could
  			 * get it */
9875cf806   David Howells   Add a dentry op t...
1010
1011
1012
1013
1014
1015
  		}
  
  		/* 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...
1016
  				break;
9875cf806   David Howells   Add a dentry op t...
1017
1018
1019
1020
1021
  			continue;
  		}
  
  		/* We didn't change the current path point */
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1022
  	}
8aef18845   Al Viro   VFS: Fix vfsmount...
1023
1024
1025
1026
1027
  
  	if (need_mntput && path->mnt == mnt)
  		mntput(path->mnt);
  	if (ret == -EISDIR)
  		ret = 0;
a3fbbde70   Al Viro   VFS: we need to s...
1028
  	return ret < 0 ? ret : need_mntput;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1029
  }
cc53ce53c   David Howells   Add a dentry op t...
1030
  int follow_down_one(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1031
1032
  {
  	struct vfsmount *mounted;
1c755af4d   Al Viro   switch lookup_mnt()
1033
  	mounted = lookup_mnt(path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1034
  	if (mounted) {
9393bd07c   Al Viro   switch follow_down()
1035
1036
1037
1038
  		dput(path->dentry);
  		mntput(path->mnt);
  		path->mnt = mounted;
  		path->dentry = dget(mounted->mnt_root);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1039
1040
1041
1042
  		return 1;
  	}
  	return 0;
  }
4d3595073   Al Viro   namei.c: move EXP...
1043
  EXPORT_SYMBOL(follow_down_one);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1044

b8faf035e   NeilBrown   VFS: allow ->d_ma...
1045
  static inline int managed_dentry_rcu(struct dentry *dentry)
62a7375e5   Ian Kent   vfs - check non-m...
1046
  {
b8faf035e   NeilBrown   VFS: allow ->d_ma...
1047
1048
  	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
  		dentry->d_op->d_manage(dentry, true) : 0;
62a7375e5   Ian Kent   vfs - check non-m...
1049
  }
9875cf806   David Howells   Add a dentry op t...
1050
  /*
287548e46   Al Viro   split __follow_mo...
1051
1052
   * 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...
1053
1054
   */
  static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
287548e46   Al Viro   split __follow_mo...
1055
  			       struct inode **inode)
9875cf806   David Howells   Add a dentry op t...
1056
  {
62a7375e5   Ian Kent   vfs - check non-m...
1057
  	for (;;) {
c71053659   Al Viro   vfs: spread struc...
1058
  		struct mount *mounted;
62a7375e5   Ian Kent   vfs - check non-m...
1059
1060
1061
1062
  		/*
  		 * Don't forget we might have a non-mountpoint managed dentry
  		 * that wants to block transit.
  		 */
b8faf035e   NeilBrown   VFS: allow ->d_ma...
1063
1064
1065
  		switch (managed_dentry_rcu(path->dentry)) {
  		case -ECHILD:
  		default:
ab90911ff   David Howells   Allow d_manage() ...
1066
  			return false;
b8faf035e   NeilBrown   VFS: allow ->d_ma...
1067
1068
1069
1070
1071
  		case -EISDIR:
  			return true;
  		case 0:
  			break;
  		}
62a7375e5   Ian Kent   vfs - check non-m...
1072
1073
  
  		if (!d_mountpoint(path->dentry))
b8faf035e   NeilBrown   VFS: allow ->d_ma...
1074
  			return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
62a7375e5   Ian Kent   vfs - check non-m...
1075

474279dc0   Al Viro   split __lookup_mn...
1076
  		mounted = __lookup_mnt(path->mnt, path->dentry);
9875cf806   David Howells   Add a dentry op t...
1077
1078
  		if (!mounted)
  			break;
c71053659   Al Viro   vfs: spread struc...
1079
1080
  		path->mnt = &mounted->mnt;
  		path->dentry = mounted->mnt.mnt_root;
a3fbbde70   Al Viro   VFS: we need to s...
1081
  		nd->flags |= LOOKUP_JUMPED;
9875cf806   David Howells   Add a dentry op t...
1082
  		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
594302624   Linus Torvalds   vfs: fix race in ...
1083
1084
1085
1086
1087
1088
  		/*
  		 * 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...
1089
  	}
b8faf035e   NeilBrown   VFS: allow ->d_ma...
1090
1091
  	return read_seqretry(&mount_lock, nd->m_seq) &&
  		!(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
287548e46   Al Viro   split __follow_mo...
1092
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1093
1094
  static int follow_dotdot_rcu(struct nameidata *nd)
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1095
  	set_root_rcu(nd);
9875cf806   David Howells   Add a dentry op t...
1096
  	while (1) {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
  		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(...
1108
  				goto failed;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1109
1110
1111
1112
1113
1114
1115
  			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 ...
1116
  	}
b37199e62   Al Viro   rcuwalk: recheck ...
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
  	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...
1128
  	nd->inode = nd->path.dentry->d_inode;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1129
  	return 0;
ef7562d52   Al Viro   make handle_dots(...
1130
1131
1132
  
  failed:
  	nd->flags &= ~LOOKUP_RCU;
5b6ca027d   Al Viro   reduce vfs_path_l...
1133
1134
  	if (!(nd->flags & LOOKUP_ROOT))
  		nd->root.mnt = NULL;
8b61e74ff   Al Viro   get rid of {lock,...
1135
  	rcu_read_unlock();
ef7562d52   Al Viro   make handle_dots(...
1136
  	return -ECHILD;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1137
  }
9875cf806   David Howells   Add a dentry op t...
1138
  /*
cc53ce53c   David Howells   Add a dentry op t...
1139
1140
1141
   * 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...
1142
   */
7cc90cc3f   Al Viro   don't pass 'mount...
1143
  int follow_down(struct path *path)
cc53ce53c   David Howells   Add a dentry op t...
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
  {
  	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() ...
1163
  			ret = path->dentry->d_op->d_manage(
1aed3e420   Al Viro   lose 'mounting_he...
1164
  				path->dentry, false);
cc53ce53c   David Howells   Add a dentry op t...
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
  			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...
1186
  EXPORT_SYMBOL(follow_down);
cc53ce53c   David Howells   Add a dentry op t...
1187
1188
  
  /*
9875cf806   David Howells   Add a dentry op t...
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
   * 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 ...
1203
  static void follow_dotdot(struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1204
  {
2a7378711   Al Viro   Cache root in nam...
1205
  	set_root(nd);
e518ddb7b   Andreas Mohr   [PATCH] fs/namei....
1206

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

2a7378711   Al Viro   Cache root in nam...
1210
1211
  		if (nd->path.dentry == nd->root.dentry &&
  		    nd->path.mnt == nd->root.mnt) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1212
1213
  			break;
  		}
4ac913785   Jan Blunck   Embed a struct pa...
1214
  		if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd708   Al Viro   Clean follow_dotd...
1215
1216
  			/* rare case of legitimate dget_parent()... */
  			nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1217
1218
1219
  			dput(old);
  			break;
  		}
3088dd708   Al Viro   Clean follow_dotd...
1220
  		if (!follow_up(&nd->path))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1221
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1222
  	}
79ed02261   Al Viro   switch follow_mou...
1223
  	follow_mount(&nd->path);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1224
  	nd->inode = nd->path.dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1225
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1226
  /*
bad611897   Miklos Szeredi   vfs: split __look...
1227
1228
1229
1230
1231
   * 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...
1232
   */
bad611897   Miklos Szeredi   vfs: split __look...
1233
  static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
201f956e4   Al Viro   fs/namei.c: don't...
1234
  				    unsigned int flags, bool *need_lookup)
baa038907   Nick Piggin   fs: dentry alloca...
1235
  {
baa038907   Nick Piggin   fs: dentry alloca...
1236
  	struct dentry *dentry;
bad611897   Miklos Szeredi   vfs: split __look...
1237
  	int error;
baa038907   Nick Piggin   fs: dentry alloca...
1238

bad611897   Miklos Szeredi   vfs: split __look...
1239
1240
1241
  	*need_lookup = false;
  	dentry = d_lookup(dir, name);
  	if (dentry) {
39e3c9553   Jeff Layton   vfs: remove DCACH...
1242
  		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
201f956e4   Al Viro   fs/namei.c: don't...
1243
  			error = d_revalidate(dentry, flags);
bad611897   Miklos Szeredi   vfs: split __look...
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
  			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...
1255

bad611897   Miklos Szeredi   vfs: split __look...
1256
1257
1258
1259
  	if (!dentry) {
  		dentry = d_alloc(dir, name);
  		if (unlikely(!dentry))
  			return ERR_PTR(-ENOMEM);
baa038907   Nick Piggin   fs: dentry alloca...
1260

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

72bd866a0   Al Viro   fs/namei.c: don't...
1283
  	old = dir->i_op->lookup(dir, dentry, flags);
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1284
1285
1286
1287
1288
1289
  	if (unlikely(old)) {
  		dput(dentry);
  		dentry = old;
  	}
  	return dentry;
  }
a32555466   Al Viro   untangling do_loo...
1290
  static struct dentry *__lookup_hash(struct qstr *name,
72bd866a0   Al Viro   fs/namei.c: don't...
1291
  		struct dentry *base, unsigned int flags)
a32555466   Al Viro   untangling do_loo...
1292
  {
bad611897   Miklos Szeredi   vfs: split __look...
1293
  	bool need_lookup;
a32555466   Al Viro   untangling do_loo...
1294
  	struct dentry *dentry;
72bd866a0   Al Viro   fs/namei.c: don't...
1295
  	dentry = lookup_dcache(name, base, flags, &need_lookup);
bad611897   Miklos Szeredi   vfs: split __look...
1296
1297
  	if (!need_lookup)
  		return dentry;
a32555466   Al Viro   untangling do_loo...
1298

72bd866a0   Al Viro   fs/namei.c: don't...
1299
  	return lookup_real(base->d_inode, dentry, flags);
a32555466   Al Viro   untangling do_loo...
1300
  }
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1301
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1302
1303
1304
1305
   *  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 ...
1306
  static int lookup_fast(struct nameidata *nd,
697f514df   Miklos Szeredi   vfs: split do_loo...
1307
  		       struct path *path, struct inode **inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1308
  {
4ac913785   Jan Blunck   Embed a struct pa...
1309
  	struct vfsmount *mnt = nd->path.mnt;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1310
  	struct dentry *dentry, *parent = nd->path.dentry;
5a18fff20   Al Viro   untangle do_lookup()
1311
1312
  	int need_reval = 1;
  	int status = 1;
9875cf806   David Howells   Add a dentry op t...
1313
  	int err;
3cac260ad   Al Viro   Take hash recalcu...
1314
  	/*
b04f784e5   Nick Piggin   fs: remove extra ...
1315
1316
1317
1318
  	 * 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 ...
1319
1320
  	if (nd->flags & LOOKUP_RCU) {
  		unsigned seq;
da53be12b   Linus Torvalds   Don't pass inode ...
1321
  		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
5a18fff20   Al Viro   untangle do_lookup()
1322
1323
  		if (!dentry)
  			goto unlazy;
12f8ad4b0   Linus Torvalds   vfs: clean up __d...
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
  		/*
  		 * 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 ...
1339
1340
  		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  			return -ECHILD;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1341
  		nd->seq = seq;
5a18fff20   Al Viro   untangle do_lookup()
1342

24643087e   Al Viro   in do_lookup() sp...
1343
  		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
4ce16ef3f   Al Viro   fs/namei.c: don't...
1344
  			status = d_revalidate(dentry, nd->flags);
5a18fff20   Al Viro   untangle do_lookup()
1345
1346
1347
1348
1349
  			if (unlikely(status <= 0)) {
  				if (status != -ECHILD)
  					need_reval = 0;
  				goto unlazy;
  			}
24643087e   Al Viro   in do_lookup() sp...
1350
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1351
1352
  		path->mnt = mnt;
  		path->dentry = dentry;
b8faf035e   NeilBrown   VFS: allow ->d_ma...
1353
1354
  		if (likely(__follow_mount_rcu(nd, path, inode)))
  			return 0;
5a18fff20   Al Viro   untangle do_lookup()
1355
  unlazy:
19660af73   Al Viro   consolidate namei...
1356
1357
  		if (unlazy_walk(nd, dentry))
  			return -ECHILD;
5a18fff20   Al Viro   untangle do_lookup()
1358
  	} else {
e97cdc87b   Al Viro   lookup_fast: get ...
1359
  		dentry = __d_lookup(parent, &nd->last);
9875cf806   David Howells   Add a dentry op t...
1360
  	}
5a18fff20   Al Viro   untangle do_lookup()
1361

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

9875cf806   David Howells   Add a dentry op t...
1377
1378
1379
  	path->mnt = mnt;
  	path->dentry = dentry;
  	err = follow_managed(path, nd->flags);
893122141   Ian Kent   vfs - fix dentry ...
1380
1381
  	if (unlikely(err < 0)) {
  		path_put_conditional(path, nd);
9875cf806   David Howells   Add a dentry op t...
1382
  		return err;
893122141   Ian Kent   vfs - fix dentry ...
1383
  	}
a3fbbde70   Al Viro   VFS: we need to s...
1384
1385
  	if (err)
  		nd->flags |= LOOKUP_JUMPED;
9875cf806   David Howells   Add a dentry op t...
1386
  	*inode = path->dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1387
  	return 0;
81e6f5208   Al Viro   untangling do_loo...
1388
1389
  
  need_lookup:
697f514df   Miklos Szeredi   vfs: split do_loo...
1390
1391
1392
1393
  	return 1;
  }
  
  /* Fast lookup failed, do it the slow way */
cc2a52711   Al Viro   lookup_slow: get ...
1394
  static int lookup_slow(struct nameidata *nd, struct path *path)
697f514df   Miklos Szeredi   vfs: split do_loo...
1395
1396
1397
1398
1399
  {
  	struct dentry *dentry, *parent;
  	int err;
  
  	parent = nd->path.dentry;
81e6f5208   Al Viro   untangling do_loo...
1400
1401
1402
  	BUG_ON(nd->inode != parent->d_inode);
  
  	mutex_lock(&parent->d_inode->i_mutex);
cc2a52711   Al Viro   lookup_slow: get ...
1403
  	dentry = __lookup_hash(&nd->last, parent, nd->flags);
81e6f5208   Al Viro   untangling do_loo...
1404
1405
1406
  	mutex_unlock(&parent->d_inode->i_mutex);
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
697f514df   Miklos Szeredi   vfs: split do_loo...
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
  	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
1417
  }
52094c8a0   Al Viro   take RCU-dependen...
1418
1419
1420
  static inline int may_lookup(struct nameidata *nd)
  {
  	if (nd->flags & LOOKUP_RCU) {
4ad5abb3d   Al Viro   no reason to keep...
1421
  		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a0   Al Viro   take RCU-dependen...
1422
1423
  		if (err != -ECHILD)
  			return err;
19660af73   Al Viro   consolidate namei...
1424
  		if (unlazy_walk(nd, NULL))
52094c8a0   Al Viro   take RCU-dependen...
1425
1426
  			return -ECHILD;
  	}
4ad5abb3d   Al Viro   no reason to keep...
1427
  	return inode_permission(nd->inode, MAY_EXEC);
52094c8a0   Al Viro   take RCU-dependen...
1428
  }
9856fa1b2   Al Viro   pull handling of ...
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
  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...
1440
1441
1442
1443
1444
1445
  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...
1446
1447
  		if (!(nd->flags & LOOKUP_ROOT))
  			nd->root.mnt = NULL;
8b61e74ff   Al Viro   get rid of {lock,...
1448
  		rcu_read_unlock();
951361f95   Al Viro   get rid of the la...
1449
1450
  	}
  }
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1451
1452
1453
1454
1455
1456
  /*
   * 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 ...
1457
  static inline int should_follow_link(struct dentry *dentry, int follow)
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1458
  {
b18825a7c   David Howells   VFS: Put a small ...
1459
  	return unlikely(d_is_symlink(dentry)) ? follow : 0;
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1460
  }
ce57dfc17   Al Viro   pull handling of ...
1461
  static inline int walk_component(struct nameidata *nd, struct path *path,
21b9b0739   Al Viro   get rid of name a...
1462
  		int follow)
ce57dfc17   Al Viro   pull handling of ...
1463
1464
1465
1466
1467
1468
1469
1470
  {
  	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...
1471
1472
  	if (unlikely(nd->last_type != LAST_NORM))
  		return handle_dots(nd, nd->last_type);
e97cdc87b   Al Viro   lookup_fast: get ...
1473
  	err = lookup_fast(nd, path, &inode);
ce57dfc17   Al Viro   pull handling of ...
1474
  	if (unlikely(err)) {
697f514df   Miklos Szeredi   vfs: split do_loo...
1475
1476
  		if (err < 0)
  			goto out_err;
cc2a52711   Al Viro   lookup_slow: get ...
1477
  		err = lookup_slow(nd, path);
697f514df   Miklos Szeredi   vfs: split do_loo...
1478
1479
1480
1481
  		if (err < 0)
  			goto out_err;
  
  		inode = path->dentry->d_inode;
ce57dfc17   Al Viro   pull handling of ...
1482
  	}
697f514df   Miklos Szeredi   vfs: split do_loo...
1483
  	err = -ENOENT;
22213318a   Al Viro   fix races between...
1484
  	if (!inode || d_is_negative(path->dentry))
697f514df   Miklos Szeredi   vfs: split do_loo...
1485
  		goto out_path_put;
b18825a7c   David Howells   VFS: Put a small ...
1486
  	if (should_follow_link(path->dentry, follow)) {
19660af73   Al Viro   consolidate namei...
1487
1488
  		if (nd->flags & LOOKUP_RCU) {
  			if (unlikely(unlazy_walk(nd, path->dentry))) {
697f514df   Miklos Szeredi   vfs: split do_loo...
1489
1490
  				err = -ECHILD;
  				goto out_err;
19660af73   Al Viro   consolidate namei...
1491
1492
  			}
  		}
ce57dfc17   Al Viro   pull handling of ...
1493
1494
1495
1496
1497
1498
  		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...
1499
1500
1501
1502
1503
1504
  
  out_path_put:
  	path_to_nameidata(path, nd);
  out_err:
  	terminate_walk(nd);
  	return err;
ce57dfc17   Al Viro   pull handling of ...
1505
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1506
  /*
b356379a0   Al Viro   Turn resolution o...
1507
1508
1509
1510
1511
1512
1513
1514
1515
   * 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...
1516
1517
1518
1519
1520
  	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 ...
1521
  	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
b356379a0   Al Viro   Turn resolution o...
1522
1523
1524
1525
1526
1527
1528
  
  	nd->depth++;
  	current->link_count++;
  
  	do {
  		struct path link = *path;
  		void *cookie;
574197e0d   Al Viro   tidy the trailing...
1529
1530
  
  		res = follow_link(&link, nd, &cookie);
6d7b5aaed   Al Viro   namei.c: let foll...
1531
1532
  		if (res)
  			break;
21b9b0739   Al Viro   get rid of name a...
1533
  		res = walk_component(nd, path, LOOKUP_FOLLOW);
574197e0d   Al Viro   tidy the trailing...
1534
  		put_link(nd, &link, cookie);
b356379a0   Al Viro   Turn resolution o...
1535
1536
1537
1538
1539
1540
1541
1542
  	} while (res > 0);
  
  	current->link_count--;
  	nd->depth--;
  	return res;
  }
  
  /*
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1543
1544
1545
1546
1547
1548
1549
   * 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...
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
   * - 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...
1560
  #include <asm/word-at-a-time.h>
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1561

f68e556e2   Linus Torvalds   Make the "word-at...
1562
  #ifdef CONFIG_64BIT
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1563
1564
1565
1566
1567
1568
1569
1570
  
  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...
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
  #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...
1581
  		a = load_unaligned_zeropad(name);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1582
1583
1584
  		if (len < sizeof(unsigned long))
  			break;
  		hash += a;
f132c5be0   Al Viro   Fix full_name_has...
1585
  		hash *= 9;
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1586
1587
1588
1589
1590
  		name += sizeof(unsigned long);
  		len -= sizeof(unsigned long);
  		if (!len)
  			goto done;
  	}
a5c21dcef   Will Deacon   dcache: allow wor...
1591
  	mask = bytemask_from_count(len);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1592
1593
1594
1595
1596
  	hash += mask & a;
  done:
  	return fold_hash(hash);
  }
  EXPORT_SYMBOL(full_name_hash);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1597
1598
1599
1600
1601
1602
  /*
   * 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...
1603
1604
  	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...
1605
1606
1607
1608
1609
1610
  
  	hash = a = 0;
  	len = -sizeof(unsigned long);
  	do {
  		hash = (hash + a) * 9;
  		len += sizeof(unsigned long);
e419b4cc5   Linus Torvalds   vfs: make word-at...
1611
  		a = load_unaligned_zeropad(name+len);
36126f8f2   Linus Torvalds   word-at-a-time: m...
1612
1613
1614
1615
1616
1617
1618
1619
1620
  		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...
1621
  	*hashp = fold_hash(hash);
36126f8f2   Linus Torvalds   word-at-a-time: m...
1622
  	return len + find_zero(mask);
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1623
1624
1625
  }
  
  #else
0145acc20   Linus Torvalds   vfs: uninline ful...
1626
1627
1628
1629
1630
1631
1632
  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_...
1633
  EXPORT_SYMBOL(full_name_hash);
0145acc20   Linus Torvalds   vfs: uninline ful...
1634

3ddcd0569   Linus Torvalds   vfs: optimize ino...
1635
  /*
200e9ef7a   Linus Torvalds   vfs: split up nam...
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
   * 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...
1653
  #endif
200e9ef7a   Linus Torvalds   vfs: split up nam...
1654
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1655
   * Name resolution.
ea3834d9f   Prasanna Meda   namei: add audit_...
1656
1657
   * 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
1658
   *
ea3834d9f   Prasanna Meda   namei: add audit_...
1659
1660
   * 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
1661
   */
6de88d729   Al Viro   kill __link_path_...
1662
  static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1663
1664
  {
  	struct path next;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1665
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1666
1667
1668
1669
  	
  	while (*name=='/')
  		name++;
  	if (!*name)
086e183a6   Al Viro   pull dropping RCU...
1670
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1671

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1672
1673
  	/* At this point we know we have a real path component. */
  	for(;;) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1674
  		struct qstr this;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1675
  		long len;
fe479a580   Al Viro   merge component t...
1676
  		int type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1677

52094c8a0   Al Viro   take RCU-dependen...
1678
  		err = may_lookup(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1679
1680
   		if (err)
  			break;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1681
  		len = hash_name(name, &this.hash);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1682
  		this.name = name;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1683
  		this.len = len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1684

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

5f4a6a695   Al Viro   link_path_walk():...
1706
1707
  		nd->last = this;
  		nd->last_type = type;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1708
  		if (!name[len])
5f4a6a695   Al Viro   link_path_walk():...
1709
  			return 0;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1710
1711
1712
1713
1714
1715
1716
1717
  		/*
  		 * 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():...
1718
  			return 0;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1719
  		name += len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1720

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

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

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

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

e41f7d4ee   Al Viro   merge path_init a...
1786
1787
1788
1789
1790
1791
1792
1793
  			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 ...
1794
  	} else {
582aa64a0   Jeff Layton   vfs: remove unnee...
1795
  		/* Caller must check execute permissions on the starting path component */
2903ff019   Al Viro   switch simple cas...
1796
  		struct fd f = fdget_raw(dfd);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1797
  		struct dentry *dentry;
2903ff019   Al Viro   switch simple cas...
1798
1799
  		if (!f.file)
  			return -EBADF;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1800

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

f52e0c113   Al Viro   New AT_... flag: ...
1803
  		if (*name) {
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
1804
  			if (!d_can_lookup(dentry)) {
2903ff019   Al Viro   switch simple cas...
1805
1806
1807
  				fdput(f);
  				return -ENOTDIR;
  			}
f52e0c113   Al Viro   New AT_... flag: ...
1808
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1809

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

31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1822
  	nd->inode = nd->path.dentry->d_inode;
9b4a9b14a   Al Viro   Preparations to c...
1823
  	return 0;
9b4a9b14a   Al Viro   Preparations to c...
1824
  }
bd92d7fed   Al Viro   Make trailing sym...
1825
1826
1827
1828
1829
1830
  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...
1831
  	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
bd92d7fed   Al Viro   Make trailing sym...
1832
  }
9b4a9b14a   Al Viro   Preparations to c...
1833
  /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
ee0827cd6   Al Viro   sanitize path_wal...
1834
  static int path_lookupat(int dfd, const char *name,
9b4a9b14a   Al Viro   Preparations to c...
1835
1836
  				unsigned int flags, struct nameidata *nd)
  {
70e9b3571   Al Viro   get rid of nd->file
1837
  	struct file *base = NULL;
bd92d7fed   Al Viro   Make trailing sym...
1838
1839
  	struct path path;
  	int err;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
  
  	/*
  	 * 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...
1855
  	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
ee0827cd6   Al Viro   sanitize path_wal...
1856

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

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

70e9b3571   Al Viro   get rid of nd->file
1890
1891
  	if (base)
  		fput(base);
ee0827cd6   Al Viro   sanitize path_wal...
1892

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

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

f78570dd6   Jeff Layton   audit: remove unn...
1910
  	if (likely(!retval))
adb5c2473   Jeff Layton   audit: make audit...
1911
  		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
170aa3d02   Ulrich Drepper   [PATCH] namei.c: ...
1912
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1913
  }
873f1eedc   Jeff Layton   vfs: turn do_path...
1914
1915
1916
1917
1918
1919
1920
  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...
1921
1922
  /* 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 ...
1923
  {
79714f72d   Al Viro   get rid of kern_p...
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
  	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...
1934
  	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
79714f72d   Al Viro   get rid of kern_p...
1935
1936
1937
1938
1939
1940
1941
  	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 ...
1942
  }
d18114657   Al Viro   [PATCH] new helpe...
1943
1944
1945
1946
1947
1948
1949
1950
  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...
1951
  EXPORT_SYMBOL(kern_path);
d18114657   Al Viro   [PATCH] new helpe...
1952

16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1953
1954
1955
1956
1957
1958
  /**
   * 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...
1959
   * @path: pointer to struct path to fill
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1960
1961
1962
   */
  int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  		    const char *name, unsigned int flags,
e0a012493   Al Viro   switch vfs_path_l...
1963
  		    struct path *path)
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1964
  {
e0a012493   Al Viro   switch vfs_path_l...
1965
1966
1967
1968
1969
  	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...
1970
  	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
e0a012493   Al Viro   switch vfs_path_l...
1971
1972
1973
1974
  	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...
1975
  }
4d3595073   Al Viro   namei.c: move EXP...
1976
  EXPORT_SYMBOL(vfs_path_lookup);
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1977

057f6c019   James Morris   security: prevent...
1978
1979
1980
1981
1982
  /*
   * 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 ...
1983
  static struct dentry *lookup_hash(struct nameidata *nd)
057f6c019   James Morris   security: prevent...
1984
  {
72bd866a0   Al Viro   fs/namei.c: don't...
1985
  	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1986
  }
eead19115   Christoph Hellwig   partially fix up ...
1987
  /**
a6b91919e   Randy Dunlap   fs: fix kernel-do...
1988
   * lookup_one_len - filesystem helper to lookup single pathname component
eead19115   Christoph Hellwig   partially fix up ...
1989
1990
1991
1992
   * @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...
1993
1994
   * 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 ...
1995
1996
1997
   * 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...
1998
1999
  struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  {
057f6c019   James Morris   security: prevent...
2000
  	struct qstr this;
6a96ba544   Al Viro   kill __lookup_one...
2001
  	unsigned int c;
cda309de2   Miklos Szeredi   vfs: move MAY_EXE...
2002
  	int err;
057f6c019   James Morris   security: prevent...
2003

2f9092e10   David Woodhouse   Fix i_mutex vs. r...
2004
  	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
6a96ba544   Al Viro   kill __lookup_one...
2005
2006
  	this.name = name;
  	this.len = len;
0145acc20   Linus Torvalds   vfs: uninline ful...
2007
  	this.hash = full_name_hash(name, len);
6a96ba544   Al Viro   kill __lookup_one...
2008
2009
  	if (!len)
  		return ERR_PTR(-EACCES);
21d8a15ac   Al Viro   lookup_one_len: d...
2010
2011
2012
2013
  	if (unlikely(name[0] == '.')) {
  		if (len < 2 || (len == 2 && name[1] == '.'))
  			return ERR_PTR(-EACCES);
  	}
6a96ba544   Al Viro   kill __lookup_one...
2014
2015
2016
2017
  	while (len--) {
  		c = *(const unsigned char *)name++;
  		if (c == '/' || c == '\0')
  			return ERR_PTR(-EACCES);
6a96ba544   Al Viro   kill __lookup_one...
2018
  	}
5a202bcd7   Al Viro   sanitize pathname...
2019
2020
2021
2022
2023
  	/*
  	 * 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 ...
2024
  		int err = base->d_op->d_hash(base, &this);
5a202bcd7   Al Viro   sanitize pathname...
2025
2026
2027
  		if (err < 0)
  			return ERR_PTR(err);
  	}
eead19115   Christoph Hellwig   partially fix up ...
2028

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

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

873f1eedc   Jeff Layton   vfs: turn do_path...
2059
2060
2061
2062
2063
2064
  /*
   * 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...
2065
  static struct filename *
9e790bd65   Jeff Layton   vfs: add a flags ...
2066
2067
  user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
  		 unsigned int flags)
2ad94ae65   Al Viro   [PATCH] new (loca...
2068
  {
91a27b2a7   Jeff Layton   vfs: define struc...
2069
  	struct filename *s = getname(path);
2ad94ae65   Al Viro   [PATCH] new (loca...
2070
  	int error;
9e790bd65   Jeff Layton   vfs: add a flags ...
2071
2072
  	/* only LOOKUP_REVAL is allowed in extra flags */
  	flags &= LOOKUP_REVAL;
2ad94ae65   Al Viro   [PATCH] new (loca...
2073
  	if (IS_ERR(s))
91a27b2a7   Jeff Layton   vfs: define struc...
2074
  		return s;
2ad94ae65   Al Viro   [PATCH] new (loca...
2075

9e790bd65   Jeff Layton   vfs: add a flags ...
2076
  	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
91a27b2a7   Jeff Layton   vfs: define struc...
2077
  	if (error) {
2ad94ae65   Al Viro   [PATCH] new (loca...
2078
  		putname(s);
91a27b2a7   Jeff Layton   vfs: define struc...
2079
2080
  		return ERR_PTR(error);
  	}
2ad94ae65   Al Viro   [PATCH] new (loca...
2081

91a27b2a7   Jeff Layton   vfs: define struc...
2082
  	return s;
2ad94ae65   Al Viro   [PATCH] new (loca...
2083
  }
8033426e6   Jeff Layton   vfs: allow umount...
2084
  /**
197df04c7   Al Viro   rename user_path_...
2085
   * mountpoint_last - look up last component for umount
8033426e6   Jeff Layton   vfs: allow umount...
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
   * @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_...
2112
  mountpoint_last(struct nameidata *nd, struct path *path)
8033426e6   Jeff Layton   vfs: allow umount...
2113
2114
2115
2116
  {
  	int error = 0;
  	struct dentry *dentry;
  	struct dentry *dir = nd->path.dentry;
35759521e   Al Viro   take unlazy_walk(...
2117
2118
2119
2120
2121
2122
  	/* 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...
2123
2124
2125
2126
2127
2128
  	}
  
  	nd->flags &= ~LOOKUP_PARENT;
  
  	if (unlikely(nd->last_type != LAST_NORM)) {
  		error = handle_dots(nd, nd->last_type);
35759521e   Al Viro   take unlazy_walk(...
2129
2130
2131
2132
  		if (error)
  			goto out;
  		dentry = dget(nd->path.dentry);
  		goto done;
8033426e6   Jeff Layton   vfs: allow umount...
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
  	}
  
  	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...
2146
  			mutex_unlock(&dir->d_inode->i_mutex);
35759521e   Al Viro   take unlazy_walk(...
2147
  			goto out;
8033426e6   Jeff Layton   vfs: allow umount...
2148
  		}
35759521e   Al Viro   take unlazy_walk(...
2149
2150
  		dentry = lookup_real(dir->d_inode, dentry, nd->flags);
  		error = PTR_ERR(dentry);
bcceeeba9   Dave Jones   Add missing unloc...
2151
2152
  		if (IS_ERR(dentry)) {
  			mutex_unlock(&dir->d_inode->i_mutex);
35759521e   Al Viro   take unlazy_walk(...
2153
  			goto out;
bcceeeba9   Dave Jones   Add missing unloc...
2154
  		}
8033426e6   Jeff Layton   vfs: allow umount...
2155
2156
  	}
  	mutex_unlock(&dir->d_inode->i_mutex);
35759521e   Al Viro   take unlazy_walk(...
2157
  done:
22213318a   Al Viro   fix races between...
2158
  	if (!dentry->d_inode || d_is_negative(dentry)) {
35759521e   Al Viro   take unlazy_walk(...
2159
2160
2161
  		error = -ENOENT;
  		dput(dentry);
  		goto out;
8033426e6   Jeff Layton   vfs: allow umount...
2162
  	}
35759521e   Al Viro   take unlazy_walk(...
2163
  	path->dentry = dentry;
295dc39d9   Vasily Averin   fs: umount on sym...
2164
  	path->mnt = nd->path.mnt;
b18825a7c   David Howells   VFS: Put a small ...
2165
  	if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
35759521e   Al Viro   take unlazy_walk(...
2166
  		return 1;
295dc39d9   Vasily Averin   fs: umount on sym...
2167
  	mntget(path->mnt);
35759521e   Al Viro   take unlazy_walk(...
2168
2169
2170
  	follow_mount(path);
  	error = 0;
  out:
8033426e6   Jeff Layton   vfs: allow umount...
2171
2172
2173
2174
2175
  	terminate_walk(nd);
  	return error;
  }
  
  /**
197df04c7   Al Viro   rename user_path_...
2176
   * path_mountpoint - look up a path to be umounted
8033426e6   Jeff Layton   vfs: allow umount...
2177
2178
   * @dfd:	directory file descriptor to start walk from
   * @name:	full pathname to walk
606d6fe3f   Randy Dunlap   fs/namei.c: fix n...
2179
   * @path:	pointer to container for result
8033426e6   Jeff Layton   vfs: allow umount...
2180
   * @flags:	lookup flags
8033426e6   Jeff Layton   vfs: allow umount...
2181
2182
   *
   * Look up the given name, but don't attempt to revalidate the last component.
606d6fe3f   Randy Dunlap   fs/namei.c: fix n...
2183
   * Returns 0 and "path" will be valid on success; Returns error otherwise.
8033426e6   Jeff Layton   vfs: allow umount...
2184
2185
   */
  static int
197df04c7   Al Viro   rename user_path_...
2186
  path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
8033426e6   Jeff Layton   vfs: allow umount...
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
  {
  	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_...
2200
  	err = mountpoint_last(&nd, path);
8033426e6   Jeff Layton   vfs: allow umount...
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
  	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_...
2211
  		err = mountpoint_last(&nd, path);
8033426e6   Jeff Layton   vfs: allow umount...
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
  		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...
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
  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...
2236
  /**
197df04c7   Al Viro   rename user_path_...
2237
   * user_path_mountpoint_at - lookup a path from userland in order to umount it
8033426e6   Jeff Layton   vfs: allow umount...
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
   * @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_...
2251
  user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
8033426e6   Jeff Layton   vfs: allow umount...
2252
2253
2254
2255
  			struct path *path)
  {
  	struct filename *s = getname(name);
  	int error;
8033426e6   Jeff Layton   vfs: allow umount...
2256
2257
  	if (IS_ERR(s))
  		return PTR_ERR(s);
2d8646510   Al Viro   introduce kern_pa...
2258
  	error = filename_mountpoint(dfd, s, path, flags);
8033426e6   Jeff Layton   vfs: allow umount...
2259
2260
2261
  	putname(s);
  	return error;
  }
2d8646510   Al Viro   introduce kern_pa...
2262
2263
2264
2265
2266
2267
2268
2269
  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
2270
2271
2272
2273
2274
2275
  /*
   * 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...
2276
  	kuid_t fsuid = current_fsuid();
da9592ede   David Howells   CRED: Wrap task c...
2277

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2278
2279
  	if (!(dir->i_mode & S_ISVTX))
  		return 0;
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
2280
  	if (uid_eq(inode->i_uid, fsuid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2281
  		return 0;
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
2282
  	if (uid_eq(dir->i_uid, fsuid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2283
  		return 0;
23adbe12e   Andy Lutomirski   fs,userns: Change...
2284
  	return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
  }
  
  /*
   *	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 ...
2306
  static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2307
  {
b18825a7c   David Howells   VFS: Put a small ...
2308
  	struct inode *inode = victim->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2309
  	int error;
b18825a7c   David Howells   VFS: Put a small ...
2310
  	if (d_is_negative(victim))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2311
  		return -ENOENT;
b18825a7c   David Howells   VFS: Put a small ...
2312
  	BUG_ON(!inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2313
2314
  
  	BUG_ON(victim->d_parent->d_inode != dir);
4fa6b5ecb   Jeff Layton   audit: overhaul _...
2315
  	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2316

f419a2e3b   Al Viro   [PATCH] kill name...
2317
  	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2318
2319
2320
2321
  	if (error)
  		return error;
  	if (IS_APPEND(dir))
  		return -EPERM;
b18825a7c   David Howells   VFS: Put a small ...
2322
2323
2324
  
  	if (check_sticky(dir, inode) || IS_APPEND(inode) ||
  	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2325
2326
  		return -EPERM;
  	if (isdir) {
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
2327
  		if (!d_is_dir(victim))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2328
2329
2330
  			return -ENOTDIR;
  		if (IS_ROOT(victim))
  			return -EBUSY;
44b1d5304   Miklos Szeredi   vfs: add d_is_dir()
2331
  	} else if (d_is_dir(victim))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
  		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: ...
2348
  static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2349
  {
14e972b45   Jeff Layton   audit: add child ...
2350
  	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2351
2352
2353
2354
  	if (child->d_inode)
  		return -EEXIST;
  	if (IS_DEADDIR(dir))
  		return -ENOENT;
f419a2e3b   Al Viro   [PATCH] kill name...
2355
  	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2356
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2357
2358
2359
2360
2361
2362
2363
2364
  /*
   * 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: ...
2365
  		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2366
2367
  		return NULL;
  	}
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
2368
  	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2369

e2761a116   OGAWA Hirofumi   [PATCH vfs-2.6 2/...
2370
2371
2372
2373
2374
  	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
2375
  	}
e2761a116   OGAWA Hirofumi   [PATCH vfs-2.6 2/...
2376
2377
2378
2379
2380
  	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
2381
  	}
f2eace23e   Ingo Molnar   [PATCH] lockdep: ...
2382
2383
  	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
2384
2385
  	return NULL;
  }
4d3595073   Al Viro   namei.c: move EXP...
2386
  EXPORT_SYMBOL(lock_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2387
2388
2389
  
  void unlock_rename(struct dentry *p1, struct dentry *p2)
  {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2390
  	mutex_unlock(&p1->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2391
  	if (p1 != p2) {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2392
  		mutex_unlock(&p2->d_inode->i_mutex);
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
2393
  		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2394
2395
  	}
  }
4d3595073   Al Viro   namei.c: move EXP...
2396
  EXPORT_SYMBOL(unlock_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2397

4acdaf27e   Al Viro   switch ->create()...
2398
  int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
312b63fba   Al Viro   don't pass nameid...
2399
  		bool want_excl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2400
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2401
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2402
2403
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
2404
  	if (!dir->i_op->create)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2405
2406
2407
2408
2409
2410
  		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...
2411
  	error = dir->i_op->create(dir, dentry, mode, want_excl);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
2412
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
2413
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2414
2415
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
2416
  EXPORT_SYMBOL(vfs_create);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2417

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

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

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

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

e1181ee65   Jeff Layton   vfs: pass struct ...
2467
  static int handle_truncate(struct file *filp)
7715b5212   Al Viro   O_TRUNC open shou...
2468
  {
e1181ee65   Jeff Layton   vfs: pass struct ...
2469
  	struct path *path = &filp->f_path;
7715b5212   Al Viro   O_TRUNC open shou...
2470
2471
2472
2473
2474
2475
2476
  	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_...
2477
  	error = locks_verify_locked(filp);
7715b5212   Al Viro   O_TRUNC open shou...
2478
  	if (!error)
ea0d3ab23   Tetsuo Handa   LSM: Remove unuse...
2479
  		error = security_path_truncate(path);
7715b5212   Al Viro   O_TRUNC open shou...
2480
2481
2482
  	if (!error) {
  		error = do_truncate(path->dentry, 0,
  				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee65   Jeff Layton   vfs: pass struct ...
2483
  				    filp);
7715b5212   Al Viro   O_TRUNC open shou...
2484
2485
  	}
  	put_write_access(inode);
acd0c9351   Mimi Zohar   IMA: update ima_c...
2486
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2487
  }
d57999e15   Dave Hansen   [PATCH] do namei_...
2488
2489
  static inline int open_to_namei_flags(int flag)
  {
8a5e929dd   Al Viro   don't translitera...
2490
2491
  	if ((flag & O_ACCMODE) == 3)
  		flag--;
d57999e15   Dave Hansen   [PATCH] do namei_...
2492
2493
  	return flag;
  }
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
  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...
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
  /*
   * 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...
2519
2520
2521
  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...
2522
  			bool got_write, bool need_lookup,
2675a4eb6   Al Viro   fs/namei.c: get d...
2523
  			int *opened)
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2524
2525
2526
2527
2528
2529
  {
  	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...
2530
2531
  	int create_error = 0;
  	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
116cc0225   Miklos Szeredi   vfs: don't set FI...
2532
  	bool excl;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2533
2534
2535
2536
2537
  
  	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...
2538
  		error = -ENOENT;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2539
2540
  		goto out;
  	}
62b259d8b   Miklos Szeredi   vfs: atomic_open(...
2541
  	mode = op->mode;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2542
2543
  	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
  		mode &= ~current_umask();
116cc0225   Miklos Szeredi   vfs: don't set FI...
2544
2545
  	excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
  	if (excl)
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2546
  		open_flag &= ~O_TRUNC;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
  
  	/*
  	 * 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...
2557
2558
2559
  	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...
2560
2561
2562
2563
2564
2565
2566
  			/*
  			 * 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...
2567
  			create_error = -EROFS;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2568
2569
2570
  			goto no_open;
  		} else {
  			/* No side effects, safe to clear O_CREAT */
64894cf84   Al Viro   simplify lookup_o...
2571
  			create_error = -EROFS;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2572
2573
2574
2575
2576
  			open_flag &= ~O_CREAT;
  		}
  	}
  
  	if (open_flag & O_CREAT) {
38227f78a   Miklos Szeredi   vfs: pass right c...
2577
  		error = may_o_create(&nd->path, dentry, mode);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
  		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
2588
2589
2590
  	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...
2591
  				      opened);
d95852777   Al Viro   make ->atomic_ope...
2592
  	if (error < 0) {
d95852777   Al Viro   make ->atomic_ope...
2593
2594
  		if (create_error && error == -ENOENT)
  			error = create_error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2595
2596
  		goto out;
  	}
d95852777   Al Viro   make ->atomic_ope...
2597
  	if (error) {	/* returned 1, that is */
30d904947   Al Viro   kill struct opendata
2598
  		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb6   Al Viro   fs/namei.c: get d...
2599
  			error = -EIO;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2600
2601
  			goto out;
  		}
30d904947   Al Viro   kill struct opendata
2602
  		if (file->f_path.dentry) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2603
  			dput(dentry);
30d904947   Al Viro   kill struct opendata
2604
  			dentry = file->f_path.dentry;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2605
  		}
03da633aa   Al Viro   atomic_open: take...
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
  		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...
2619
  		}
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2620
2621
2622
2623
2624
2625
2626
  		goto looked_up;
  	}
  
  	/*
  	 * We didn't have the inode before the open, so check open permission
  	 * here.
  	 */
03da633aa   Al Viro   atomic_open: take...
2627
2628
2629
2630
2631
2632
  	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...
2633
2634
2635
  	error = may_open(&file->f_path, acc_mode, open_flag);
  	if (error)
  		fput(file);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2636
2637
2638
  
  out:
  	dput(dentry);
2675a4eb6   Al Viro   fs/namei.c: get d...
2639
  	return error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2640

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2641
2642
  no_open:
  	if (need_lookup) {
72bd866a0   Al Viro   fs/namei.c: don't...
2643
  		dentry = lookup_real(dir, dentry, nd->flags);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2644
  		if (IS_ERR(dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2645
  			return PTR_ERR(dentry);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2646
2647
2648
  
  		if (create_error) {
  			int open_flag = op->open_flag;
2675a4eb6   Al Viro   fs/namei.c: get d...
2649
  			error = create_error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
  			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...
2665
  	return 1;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2666
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2667
  /*
1acf0af9b   David Howells   VFS: Fix the bann...
2668
   * Look up and maybe create and open the last component.
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2669
2670
2671
   *
   * Must be called with i_mutex held on parent.
   *
1acf0af9b   David Howells   VFS: Fix the bann...
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
   * 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...
2684
   */
2675a4eb6   Al Viro   fs/namei.c: get d...
2685
2686
2687
  static int lookup_open(struct nameidata *nd, struct path *path,
  			struct file *file,
  			const struct open_flags *op,
64894cf84   Al Viro   simplify lookup_o...
2688
  			bool got_write, int *opened)
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2689
2690
  {
  	struct dentry *dir = nd->path.dentry;
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2691
  	struct inode *dir_inode = dir->d_inode;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2692
2693
  	struct dentry *dentry;
  	int error;
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2694
  	bool need_lookup;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2695

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

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2701
2702
2703
2704
2705
  	/* 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...
2706
  		return atomic_open(nd, dentry, path, file, op, got_write,
47237687d   Al Viro   ->atomic_open() p...
2707
  				   need_lookup, opened);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2708
  	}
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2709
2710
  	if (need_lookup) {
  		BUG_ON(dentry->d_inode);
72bd866a0   Al Viro   fs/namei.c: don't...
2711
  		dentry = lookup_real(dir_inode, dentry, nd->flags);
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2712
  		if (IS_ERR(dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2713
  			return PTR_ERR(dentry);
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2714
  	}
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
  	/* 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 ...
2725
  		 * the 'struct file' in finish_open().
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2726
  		 */
64894cf84   Al Viro   simplify lookup_o...
2727
2728
  		if (!got_write) {
  			error = -EROFS;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2729
  			goto out_dput;
64894cf84   Al Viro   simplify lookup_o...
2730
  		}
47237687d   Al Viro   ->atomic_open() p...
2731
  		*opened |= FILE_CREATED;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2732
2733
2734
  		error = security_path_mknod(&nd->path, dentry, mode, 0);
  		if (error)
  			goto out_dput;
312b63fba   Al Viro   don't pass nameid...
2735
2736
  		error = vfs_create(dir->d_inode, dentry, mode,
  				   nd->flags & LOOKUP_EXCL);
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2737
2738
2739
  		if (error)
  			goto out_dput;
  	}
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2740
  out_no_open:
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2741
2742
  	path->dentry = dentry;
  	path->mnt = nd->path.mnt;
2675a4eb6   Al Viro   fs/namei.c: get d...
2743
  	return 1;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2744
2745
2746
  
  out_dput:
  	dput(dentry);
2675a4eb6   Al Viro   fs/namei.c: get d...
2747
  	return error;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2748
2749
2750
  }
  
  /*
fe2d35ff0   Al Viro   switch non-create...
2751
   * Handle the last step of open()
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2752
   */
2675a4eb6   Al Viro   fs/namei.c: get d...
2753
2754
  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...
2755
  		   int *opened, struct filename *name)
fb1cc555d   Al Viro   gut do_filp_open(...
2756
  {
a1e28038d   Al Viro   pull the common p...
2757
  	struct dentry *dir = nd->path.dentry;
ca344a894   Al Viro   do_last: unify ma...
2758
  	int open_flag = op->open_flag;
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2759
  	bool will_truncate = (open_flag & O_TRUNC) != 0;
64894cf84   Al Viro   simplify lookup_o...
2760
  	bool got_write = false;
bcda76524   Al Viro   Allow O_PATH for ...
2761
  	int acc_mode = op->acc_mode;
a1eb33153   Miklos Szeredi   vfs: do_last(): i...
2762
  	struct inode *inode;
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2763
  	bool symlink_ok = false;
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2764
2765
  	struct path save_parent = { .dentry = NULL, .mnt = NULL };
  	bool retried = false;
16c2cd717   Al Viro   untangle the "nee...
2766
  	int error;
1f36f774b   Al Viro   Switch !O_CREAT c...
2767

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

ca344a894   Al Viro   do_last: unify ma...
2777
  	if (!(open_flag & O_CREAT)) {
fe2d35ff0   Al Viro   switch non-create...
2778
2779
  		if (nd->last.name[nd->last.len])
  			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
bcda76524   Al Viro   Allow O_PATH for ...
2780
  		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2781
  			symlink_ok = true;
fe2d35ff0   Al Viro   switch non-create...
2782
  		/* we _can_ be in RCU mode here */
e97cdc87b   Al Viro   lookup_fast: get ...
2783
  		error = lookup_fast(nd, path, &inode);
715748654   Miklos Szeredi   vfs: do_last(): c...
2784
2785
2786
2787
  		if (likely(!error))
  			goto finish_lookup;
  
  		if (error < 0)
2675a4eb6   Al Viro   fs/namei.c: get d...
2788
  			goto out;
715748654   Miklos Szeredi   vfs: do_last(): c...
2789
2790
  
  		BUG_ON(nd->inode != dir->d_inode);
b6183df7b   Miklos Szeredi   vfs: do_last(): s...
2791
2792
2793
2794
2795
2796
2797
2798
2799
  	} 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...
2800
  			return error;
fe2d35ff0   Al Viro   switch non-create...
2801

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

16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2809
  retry_lookup:
64894cf84   Al Viro   simplify lookup_o...
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
  	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...
2820
  	mutex_lock(&dir->d_inode->i_mutex);
64894cf84   Al Viro   simplify lookup_o...
2821
  	error = lookup_open(nd, path, file, op, got_write, opened);
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2822
  	mutex_unlock(&dir->d_inode->i_mutex);
a1e28038d   Al Viro   pull the common p...
2823

2675a4eb6   Al Viro   fs/namei.c: get d...
2824
2825
  	if (error <= 0) {
  		if (error)
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2826
  			goto out;
47237687d   Al Viro   ->atomic_open() p...
2827
  		if ((*opened & FILE_CREATED) ||
496ad9aa8   Al Viro   new helper: file_...
2828
  		    !S_ISREG(file_inode(file)->i_mode))
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2829
  			will_truncate = false;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2830

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

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

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2850
2851
2852
2853
2854
  	/*
  	 * 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...
2855
  	if (got_write) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2856
  		mnt_drop_write(nd->path.mnt);
64894cf84   Al Viro   simplify lookup_o...
2857
  		got_write = false;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2858
  	}
fb1cc555d   Al Viro   gut do_filp_open(...
2859
  	error = -EEXIST;
f8310c592   Al Viro   fix O_EXCL handli...
2860
  	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
fb1cc555d   Al Viro   gut do_filp_open(...
2861
  		goto exit_dput;
9875cf806   David Howells   Add a dentry op t...
2862
2863
2864
  	error = follow_managed(path, nd->flags);
  	if (error < 0)
  		goto exit_dput;
fb1cc555d   Al Viro   gut do_filp_open(...
2865

a3fbbde70   Al Viro   VFS: we need to s...
2866
2867
  	if (error)
  		nd->flags |= LOOKUP_JUMPED;
decf34008   Miklos Szeredi   vfs: do_last(): u...
2868
2869
  	BUG_ON(nd->flags & LOOKUP_RCU);
  	inode = path->dentry->d_inode;
5f5daac12   Miklos Szeredi   vfs: do_last() co...
2870
2871
  finish_lookup:
  	/* we _can_ be in RCU mode here */
fb1cc555d   Al Viro   gut do_filp_open(...
2872
  	error = -ENOENT;
22213318a   Al Viro   fix races between...
2873
  	if (!inode || d_is_negative(path->dentry)) {
54c33e7f9   Miklos Szeredi   vfs: do_last(): m...
2874
  		path_to_nameidata(path, nd);
2675a4eb6   Al Viro   fs/namei.c: get d...
2875
  		goto out;
54c33e7f9   Miklos Szeredi   vfs: do_last(): m...
2876
  	}
9e67f3616   Al Viro   Kill is_link argu...
2877

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

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

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

fb1cc555d   Al Viro   gut do_filp_open(...
2952
2953
  exit_dput:
  	path_put_conditional(path, nd);
ca344a894   Al Viro   do_last: unify ma...
2954
  	goto out;
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2955
  exit_fput:
2675a4eb6   Al Viro   fs/namei.c: get d...
2956
2957
  	fput(file);
  	goto out;
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2958

f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
  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...
2970
  	if (got_write) {
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2971
  		mnt_drop_write(nd->path.mnt);
64894cf84   Al Viro   simplify lookup_o...
2972
  		got_write = false;
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2973
2974
2975
  	}
  	retried = true;
  	goto retry_lookup;
fb1cc555d   Al Viro   gut do_filp_open(...
2976
  }
60545d0d4   Al Viro   [O_TMPFILE] it's ...
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
3016
3017
3018
3019
3020
3021
3022
  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...
3023
  	if (error) {
60545d0d4   Al Viro   [O_TMPFILE] it's ...
3024
  		fput(file);
f4e0c30c1   Al Viro   allow the temp fi...
3025
3026
3027
3028
3029
3030
  	} 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 ...
3031
3032
3033
3034
3035
3036
  out2:
  	mnt_drop_write(nd->path.mnt);
  out:
  	path_put(&nd->path);
  	return error;
  }
669abf4e5   Jeff Layton   vfs: make path_op...
3037
  static struct file *path_openat(int dfd, struct filename *pathname,
73d049a40   Al Viro   open-style analog...
3038
  		struct nameidata *nd, const struct open_flags *op, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3039
  {
fe2d35ff0   Al Viro   switch non-create...
3040
  	struct file *base = NULL;
30d904947   Al Viro   kill struct opendata
3041
  	struct file *file;
9850c0565   Al Viro   Fix the -ESTALE h...
3042
  	struct path path;
47237687d   Al Viro   ->atomic_open() p...
3043
  	int opened = 0;
13aab428a   Al Viro   separate -ESTALE/...
3044
  	int error;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
3045

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

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

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

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

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

c663e5d80   Christoph Hellwig   [PATCH] add some ...
3157
3158
3159
3160
  	/*
  	 * Yucky last component or no last component at all?
  	 * (foo/., foo/.., /////)
  	 */
ed75e95de   Al Viro   kill lookup_create()
3161
3162
3163
3164
  	if (nd.last_type != LAST_NORM)
  		goto out;
  	nd.flags &= ~LOOKUP_PARENT;
  	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
3165

c30dabfe5   Jan Kara   fs: Push mnt_want...
3166
3167
  	/* 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 ...
3168
3169
3170
  	/*
  	 * Do the final lookup.
  	 */
ed75e95de   Al Viro   kill lookup_create()
3171
3172
  	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
3173
  	if (IS_ERR(dentry))
a8104a9fc   Al Viro   pull mnt_want_wri...
3174
  		goto unlock;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
3175

a8104a9fc   Al Viro   pull mnt_want_wri...
3176
  	error = -EEXIST;
b18825a7c   David Howells   VFS: Put a small ...
3177
  	if (d_is_positive(dentry))
a8104a9fc   Al Viro   pull mnt_want_wri...
3178
  		goto fail;
b18825a7c   David Howells   VFS: Put a small ...
3179

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

f69aac000   Al Viro   switch may_mknod(...
3251
  static int may_mknod(umode_t mode)
463c31972   Dave Hansen   [PATCH] r/o bind ...
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
  {
  	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...
3267
  SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3268
  		unsigned, dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3269
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3270
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3271
3272
  	struct path path;
  	int error;
972567f14   Jeff Layton   vfs: fix mknodat ...
3273
  	unsigned int lookup_flags = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3274

8e4bfca1d   Al Viro   mknod: take sanit...
3275
3276
3277
  	error = may_mknod(mode);
  	if (error)
  		return error;
972567f14   Jeff Layton   vfs: fix mknodat ...
3278
3279
  retry:
  	dentry = user_path_create(dfd, filename, &path, lookup_flags);
dae6ad8f3   Al Viro   new helpers: kern...
3280
3281
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
2ad94ae65   Al Viro   [PATCH] new (loca...
3282

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

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

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

dae6ad8f3   Al Viro   new helpers: kern...
3347
  	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5   Al Viro   New helper - curr...
3348
  		mode &= ~current_umask();
dae6ad8f3   Al Viro   new helpers: kern...
3349
  	error = security_path_mkdir(&path, dentry, mode);
a8104a9fc   Al Viro   pull mnt_want_wri...
3350
3351
  	if (!error)
  		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
921a1650d   Al Viro   new helper: done_...
3352
  	done_path_create(&path, dentry);
b76d8b822   Jeff Layton   vfs: fix mkdirat ...
3353
3354
3355
3356
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3357
3358
  	return error;
  }
a218d0fdc   Al Viro   switch open and m...
3359
  SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3360
3361
3362
  {
  	return sys_mkdirat(AT_FDCWD, pathname, mode);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3363
  /*
a71905f0d   Sage Weil   vfs: update dentr...
3364
   * The dentry_unhash() helper will try to drop the dentry early: we
c0d025948   J. Bruce Fields   vfs: fix out-of-d...
3365
   * should have a usage count of 1 if we're the only user of this
a71905f0d   Sage Weil   vfs: update dentr...
3366
3367
   * 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
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
   *
   * 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...
3380
  	shrink_dcache_parent(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3381
  	spin_lock(&dentry->d_lock);
98474236f   Waiman Long   vfs: make the den...
3382
  	if (dentry->d_lockref.count == 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3383
3384
  		__d_drop(dentry);
  	spin_unlock(&dentry->d_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3385
  }
4d3595073   Al Viro   namei.c: move EXP...
3386
  EXPORT_SYMBOL(dentry_unhash);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3387
3388
3389
3390
3391
3392
3393
  
  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...
3394
  	if (!dir->i_op->rmdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3395
  		return -EPERM;
1d2ef5901   Al Viro   restore pinning t...
3396
  	dget(dentry);
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3397
  	mutex_lock(&dentry->d_inode->i_mutex);
912dbc15d   Sage Weil   vfs: clean up vfs...
3398
3399
  
  	error = -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3400
  	if (d_mountpoint(dentry))
912dbc15d   Sage Weil   vfs: clean up vfs...
3401
3402
3403
3404
3405
  		goto out;
  
  	error = security_inode_rmdir(dir, dentry);
  	if (error)
  		goto out;
3cebde241   Sage Weil   vfs: shrink_dcach...
3406
  	shrink_dcache_parent(dentry);
912dbc15d   Sage Weil   vfs: clean up vfs...
3407
3408
3409
3410
3411
3412
3413
3414
  	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...
3415
  	mutex_unlock(&dentry->d_inode->i_mutex);
1d2ef5901   Al Viro   restore pinning t...
3416
  	dput(dentry);
912dbc15d   Sage Weil   vfs: clean up vfs...
3417
  	if (!error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3418
  		d_delete(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3419
3420
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3421
  EXPORT_SYMBOL(vfs_rmdir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3422

5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3423
  static long do_rmdir(int dfd, const char __user *pathname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3424
3425
  {
  	int error = 0;
91a27b2a7   Jeff Layton   vfs: define struc...
3426
  	struct filename *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3427
3428
  	struct dentry *dentry;
  	struct nameidata nd;
c6ee92069   Jeff Layton   vfs: make do_rmdi...
3429
3430
3431
  	unsigned int lookup_flags = 0;
  retry:
  	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
3432
3433
  	if (IS_ERR(name))
  		return PTR_ERR(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3434
3435
  
  	switch(nd.last_type) {
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3436
3437
3438
3439
3440
3441
3442
3443
3444
  	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
3445
  	}
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3446
3447
  
  	nd.flags &= ~LOOKUP_PARENT;
c30dabfe5   Jan Kara   fs: Push mnt_want...
3448
3449
3450
  	error = mnt_want_write(nd.path.mnt);
  	if (error)
  		goto exit1;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3451

4ac913785   Jan Blunck   Embed a struct pa...
3452
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
3453
  	dentry = lookup_hash(&nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3454
  	error = PTR_ERR(dentry);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3455
3456
  	if (IS_ERR(dentry))
  		goto exit2;
e6bc45d65   Theodore Ts'o   vfs: make unlink(...
3457
3458
3459
3460
  	if (!dentry->d_inode) {
  		error = -ENOENT;
  		goto exit3;
  	}
be6d3e56a   Kentaro Takeda   introduce new LSM...
3461
3462
  	error = security_path_rmdir(&nd.path, dentry);
  	if (error)
c30dabfe5   Jan Kara   fs: Push mnt_want...
3463
  		goto exit3;
4ac913785   Jan Blunck   Embed a struct pa...
3464
  	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
0622753b8   Dave Hansen   [PATCH] r/o bind ...
3465
  exit3:
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3466
3467
  	dput(dentry);
  exit2:
4ac913785   Jan Blunck   Embed a struct pa...
3468
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
c30dabfe5   Jan Kara   fs: Push mnt_want...
3469
  	mnt_drop_write(nd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3470
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
3471
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3472
  	putname(name);
c6ee92069   Jeff Layton   vfs: make do_rmdi...
3473
3474
3475
3476
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3477
3478
  	return error;
  }
3cdad4288   Heiko Carstens   [CVE-2009-0029] S...
3479
  SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3480
3481
3482
  {
  	return do_rmdir(AT_FDCWD, pathname);
  }
b21996e36   J. Bruce Fields   locks: break dele...
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
  /**
   * 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
3502
  {
9accbb977   J. Bruce Fields   namei: minor vfs_...
3503
  	struct inode *target = dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3504
3505
3506
3507
  	int error = may_delete(dir, dentry, 0);
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3508
  	if (!dir->i_op->unlink)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3509
  		return -EPERM;
9accbb977   J. Bruce Fields   namei: minor vfs_...
3510
  	mutex_lock(&target->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3511
3512
3513
3514
  	if (d_mountpoint(dentry))
  		error = -EBUSY;
  	else {
  		error = security_inode_unlink(dir, dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3515
  		if (!error) {
5a14696c1   J. Bruce Fields   locks: helper fun...
3516
3517
  			error = try_break_deleg(target, delegated_inode);
  			if (error)
b21996e36   J. Bruce Fields   locks: break dele...
3518
  				goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3519
  			error = dir->i_op->unlink(dir, dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3520
  			if (!error)
d83c49f3e   Al Viro   Fix the regressio...
3521
  				dont_mount(dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3522
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3523
  	}
b21996e36   J. Bruce Fields   locks: break dele...
3524
  out:
9accbb977   J. Bruce Fields   namei: minor vfs_...
3525
  	mutex_unlock(&target->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3526
3527
3528
  
  	/* 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_...
3529
  		fsnotify_link_count(target);
e234f35c5   John McCutchan   [PATCH] inotify d...
3530
  		d_delete(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3531
  	}
0eeca2830   Robert Love   [PATCH] inotify
3532

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3533
3534
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3535
  EXPORT_SYMBOL(vfs_unlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3536
3537
3538
  
  /*
   * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3539
   * directory's i_mutex.  Truncate can take a long time if there is a lot of
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3540
3541
3542
   * 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 ...
3543
  static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3544
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3545
  	int error;
91a27b2a7   Jeff Layton   vfs: define struc...
3546
  	struct filename *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3547
3548
3549
  	struct dentry *dentry;
  	struct nameidata nd;
  	struct inode *inode = NULL;
b21996e36   J. Bruce Fields   locks: break dele...
3550
  	struct inode *delegated_inode = NULL;
5d18f8133   Jeff Layton   vfs: make do_unli...
3551
3552
3553
  	unsigned int lookup_flags = 0;
  retry:
  	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
3554
3555
  	if (IS_ERR(name))
  		return PTR_ERR(name);
2ad94ae65   Al Viro   [PATCH] new (loca...
3556

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

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

91a27b2a7   Jeff Layton   vfs: define struc...
3664
  	error = security_path_symlink(&path, dentry, from->name);
a8104a9fc   Al Viro   pull mnt_want_wri...
3665
  	if (!error)
91a27b2a7   Jeff Layton   vfs: define struc...
3666
  		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
921a1650d   Al Viro   new helper: done_...
3667
  	done_path_create(&path, dentry);
f46d3567b   Jeff Layton   vfs: fix symlinka...
3668
3669
3670
3671
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3672
  out_putname:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3673
3674
3675
  	putname(from);
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
3676
  SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3677
3678
3679
  {
  	return sys_symlinkat(oldname, AT_FDCWD, newname);
  }
146a8595c   J. Bruce Fields   locks: break dele...
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
  /**
   * 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
3700
3701
  {
  	struct inode *inode = old_dentry->d_inode;
8de527787   Al Viro   vfs: check i_nlin...
3702
  	unsigned max_links = dir->i_sb->s_max_links;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3703
3704
3705
3706
  	int error;
  
  	if (!inode)
  		return -ENOENT;
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3707
  	error = may_create(dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
  	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...
3719
  	if (!dir->i_op->link)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3720
  		return -EPERM;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3721
  	if (S_ISDIR(inode->i_mode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3722
3723
3724
3725
3726
  		return -EPERM;
  
  	error = security_inode_link(old_dentry, dir, new_dentry);
  	if (error)
  		return error;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3727
  	mutex_lock(&inode->i_mutex);
aae8a97d3   Aneesh Kumar K.V   fs: Don't allow t...
3728
  	/* Make sure we don't allow creating hardlink to an unlinked file */
f4e0c30c1   Al Viro   allow the temp fi...
3729
  	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
aae8a97d3   Aneesh Kumar K.V   fs: Don't allow t...
3730
  		error =  -ENOENT;
8de527787   Al Viro   vfs: check i_nlin...
3731
3732
  	else if (max_links && inode->i_nlink >= max_links)
  		error = -EMLINK;
146a8595c   J. Bruce Fields   locks: break dele...
3733
3734
3735
3736
3737
  	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...
3738
3739
3740
3741
3742
3743
  
  	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: ...
3744
  	mutex_unlock(&inode->i_mutex);
e31e14ec3   Stephen Smalley   [PATCH] remove th...
3745
  	if (!error)
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3746
  		fsnotify_link(dir, inode, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3747
3748
  	return error;
  }
4d3595073   Al Viro   namei.c: move EXP...
3749
  EXPORT_SYMBOL(vfs_link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
  
  /*
   * 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...
3760
3761
  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
3762
3763
  {
  	struct dentry *new_dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3764
  	struct path old_path, new_path;
146a8595c   J. Bruce Fields   locks: break dele...
3765
  	struct inode *delegated_inode = NULL;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3766
  	int how = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3767
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3768

11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3769
  	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e16   Ulrich Drepper   [PATCH] flags par...
3770
  		return -EINVAL;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3771
  	/*
f0cc6ffb8   Linus Torvalds   Revert "fs: Allow...
3772
3773
3774
  	 * 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...
3775
  	 */
f0cc6ffb8   Linus Torvalds   Revert "fs: Allow...
3776
3777
3778
  	if (flags & AT_EMPTY_PATH) {
  		if (!capable(CAP_DAC_READ_SEARCH))
  			return -ENOENT;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3779
  		how = LOOKUP_EMPTY;
f0cc6ffb8   Linus Torvalds   Revert "fs: Allow...
3780
  	}
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3781
3782
3783
  
  	if (flags & AT_SYMLINK_FOLLOW)
  		how |= LOOKUP_FOLLOW;
442e31ca5   Jeff Layton   vfs: fix linkat t...
3784
  retry:
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3785
  	error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3786
  	if (error)
2ad94ae65   Al Viro   [PATCH] new (loca...
3787
  		return error;
442e31ca5   Jeff Layton   vfs: fix linkat t...
3788
3789
  	new_dentry = user_path_create(newdfd, newname, &new_path,
  					(how & LOOKUP_REVAL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3790
  	error = PTR_ERR(new_dentry);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3791
  	if (IS_ERR(new_dentry))
dae6ad8f3   Al Viro   new helpers: kern...
3792
3793
3794
3795
3796
  		goto out;
  
  	error = -EXDEV;
  	if (old_path.mnt != new_path.mnt)
  		goto out_dput;
800179c9b   Kees Cook   fs: add link rest...
3797
3798
3799
  	error = may_linkat(&old_path);
  	if (unlikely(error))
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
3800
  	error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56a   Kentaro Takeda   introduce new LSM...
3801
  	if (error)
a8104a9fc   Al Viro   pull mnt_want_wri...
3802
  		goto out_dput;
146a8595c   J. Bruce Fields   locks: break dele...
3803
  	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
3804
  out_dput:
921a1650d   Al Viro   new helper: done_...
3805
  	done_path_create(&new_path, new_dentry);
146a8595c   J. Bruce Fields   locks: break dele...
3806
3807
  	if (delegated_inode) {
  		error = break_deleg_wait(&delegated_inode);
d22e6338d   Oleg Drokin   Fix mountpoint re...
3808
3809
  		if (!error) {
  			path_put(&old_path);
146a8595c   J. Bruce Fields   locks: break dele...
3810
  			goto retry;
d22e6338d   Oleg Drokin   Fix mountpoint re...
3811
  		}
146a8595c   J. Bruce Fields   locks: break dele...
3812
  	}
442e31ca5   Jeff Layton   vfs: fix linkat t...
3813
  	if (retry_estale(error, how)) {
d22e6338d   Oleg Drokin   Fix mountpoint re...
3814
  		path_put(&old_path);
442e31ca5   Jeff Layton   vfs: fix linkat t...
3815
3816
3817
  		how |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3818
  out:
2d8f30380   Al Viro   [PATCH] sanitize ...
3819
  	path_put(&old_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3820
3821
3822
  
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
3823
  SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3824
  {
c04030e16   Ulrich Drepper   [PATCH] flags par...
3825
  	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3826
  }
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3827
3828
3829
3830
3831
3832
3833
  /**
   * 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...
3834
   * @flags:	rename flags
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
   *
   * 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
3849
3850
3851
   * 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:
d03b29a27   J. Bruce Fields   namei: trivial fi...
3852
   *	a) we can get into loop creation.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3853
3854
   *	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...
3855
   *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3856
   *	   story.
6cedba896   J. Bruce Fields   vfs: take i_mutex...
3857
3858
   *	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...
3859
   *	   And that - after we got ->i_mutex on parents (until then we don't know
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3860
3861
   *	   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...
3862
   *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3863
3864
3865
   *	   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...
3866
   *	   lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3867
3868
3869
   *	   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_...
3870
   *	d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3871
   *	   we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3872
   *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e72   Adam Buchbinder   Fix misspellings ...
3873
   *	   ->i_mutex on parents, which works but leads to some truly excessive
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3874
3875
   *	   locking].
   */
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3876
3877
  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...
3878
  	       struct inode **delegated_inode, unsigned int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3879
  {
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3880
3881
3882
3883
  	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...
3884
  	struct inode *target = new_dentry->d_inode;
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3885
3886
  	bool new_is_dir = false;
  	unsigned max_links = new_dir->i_sb->s_max_links;
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3887
3888
3889
3890
3891
3892
3893
  
  	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...
3894
  	if (!target) {
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3895
  		error = may_create(new_dir, new_dentry);
da1ce0670   Miklos Szeredi   vfs: add cross-re...
3896
3897
3898
3899
3900
3901
3902
3903
  	} 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 ...
3904
3905
  	if (error)
  		return error;
7177a9c4b   Miklos Szeredi   fs: call rename2 ...
3906
  	if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
bc27027a7   Miklos Szeredi   vfs: rename: use ...
3907
  		return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3908

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

520c8b165   Miklos Szeredi   vfs: add renameat...
4001
4002
  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
4003
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
4004
4005
4006
  	struct dentry *old_dir, *new_dir;
  	struct dentry *old_dentry, *new_dentry;
  	struct dentry *trap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4007
  	struct nameidata oldnd, newnd;
8e6d782ca   J. Bruce Fields   locks: break dele...
4008
  	struct inode *delegated_inode = NULL;
91a27b2a7   Jeff Layton   vfs: define struc...
4009
4010
  	struct filename *from;
  	struct filename *to;
c6a942840   Jeff Layton   vfs: fix renameat...
4011
4012
  	unsigned int lookup_flags = 0;
  	bool should_retry = false;
2ad94ae65   Al Viro   [PATCH] new (loca...
4013
  	int error;
520c8b165   Miklos Szeredi   vfs: add renameat...
4014

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

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4169
  	nd.depth = 0;
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4170
  	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
4171
4172
  	if (IS_ERR(cookie))
  		return PTR_ERR(cookie);
5d826c847   Al Viro   new helper: readl...
4173
  	res = readlink_copy(buffer, buflen, nd_get_link(&nd));
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
4174
4175
4176
  	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
4177
  }
4d3595073   Al Viro   namei.c: move EXP...
4178
  EXPORT_SYMBOL(generic_readlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4179

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

cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4207
  void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4208
  {
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4209
  	struct page *page = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4210
  	nd_set_link(nd, page_getlink(dentry, &page));
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4211
  	return page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4212
  }
4d3595073   Al Viro   namei.c: move EXP...
4213
  EXPORT_SYMBOL(page_follow_link_light);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4214

cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4215
  void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4216
  {
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
4217
4218
4219
  	struct page *page = cookie;
  
  	if (page) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4220
4221
  		kunmap(page);
  		page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4222
4223
  	}
  }
4d3595073   Al Viro   namei.c: move EXP...
4224
  EXPORT_SYMBOL(page_put_link);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4225

54566b2c1   Nick Piggin   fs: symlink write...
4226
4227
4228
4229
  /*
   * 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
4230
4231
  {
  	struct address_space *mapping = inode->i_mapping;
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
4232
  	struct page *page;
afddba49d   Nick Piggin   fs: introduce wri...
4233
  	void *fsdata;
beb497ab4   Dmitriy Monakhov   [PATCH] __page_sy...
4234
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4235
  	char *kaddr;
54566b2c1   Nick Piggin   fs: symlink write...
4236
4237
4238
  	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  	if (nofs)
  		flags |= AOP_FLAG_NOFS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4239

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

0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
4262
4263
4264
  int page_symlink(struct inode *inode, const char *symname, int len)
  {
  	return __page_symlink(inode, symname, len,
54566b2c1   Nick Piggin   fs: symlink write...
4265
  			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
4266
  }
4d3595073   Al Viro   namei.c: move EXP...
4267
  EXPORT_SYMBOL(page_symlink);
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
4268

92e1d5be9   Arjan van de Ven   [PATCH] mark stru...
4269
  const struct inode_operations page_symlink_inode_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4270
4271
4272
4273
  	.readlink	= generic_readlink,
  	.follow_link	= page_follow_link_light,
  	.put_link	= page_put_link,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4274
  EXPORT_SYMBOL(page_symlink_inode_operations);