Blame view

fs/namei.c 99.8 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
193
194
  	if (unlikely(len >= PATH_MAX))
  		goto error;
  
  	result->uptr = filename;
  	audit_getname(result);
  	return result;
3f9f0aa68   Linus Torvalds   VFS: clean up and...
195
196
  
  error:
7950e3852   Jeff Layton   vfs: embed struct...
197
  	final_putname(result);
3f9f0aa68   Linus Torvalds   VFS: clean up and...
198
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
  }
91a27b2a7   Jeff Layton   vfs: define struc...
200
201
  struct filename *
  getname(const char __user * filename)
f52e0c113   Al Viro   New AT_... flag: ...
202
  {
f7493e5d9   Linus Torvalds   vfs: tidy up spar...
203
  	return getname_flags(filename, 0, NULL);
f52e0c113   Al Viro   New AT_... flag: ...
204
  }
91a27b2a7   Jeff Layton   vfs: define struc...
205
  EXPORT_SYMBOL(getname);
f52e0c113   Al Viro   New AT_... flag: ...
206

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
  #ifdef CONFIG_AUDITSYSCALL
91a27b2a7   Jeff Layton   vfs: define struc...
208
  void putname(struct filename *name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
  {
5ac3a9c26   Al Viro   [PATCH] don't bot...
210
  	if (unlikely(!audit_dummy_context()))
91a27b2a7   Jeff Layton   vfs: define struc...
211
212
  		return audit_putname(name);
  	final_putname(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  #endif
e77819e57   Linus Torvalds   vfs: move ACL cac...
215
216
  static int check_acl(struct inode *inode, int mask)
  {
84635d68b   Linus Torvalds   vfs: fix check_ac...
217
  #ifdef CONFIG_FS_POSIX_ACL
e77819e57   Linus Torvalds   vfs: move ACL cac...
218
  	struct posix_acl *acl;
e77819e57   Linus Torvalds   vfs: move ACL cac...
219
  	if (mask & MAY_NOT_BLOCK) {
3567866bf   Al Viro   RCUify freeing ac...
220
221
  		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  	        if (!acl)
e77819e57   Linus Torvalds   vfs: move ACL cac...
222
  	                return -EAGAIN;
3567866bf   Al Viro   RCUify freeing ac...
223
224
225
  		/* no ->get_acl() calls in RCU mode... */
  		if (acl == ACL_NOT_CACHED)
  			return -ECHILD;
206b1d09a   Ari Savolainen   Fix POSIX ACL per...
226
  	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
e77819e57   Linus Torvalds   vfs: move ACL cac...
227
228
229
230
231
  	}
  
  	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
  
  	/*
4e34e719e   Christoph Hellwig   fs: take the ACL ...
232
233
234
  	 * A filesystem can force a ACL callback by just never filling the
  	 * ACL cache. But normally you'd fill the cache either at inode
  	 * instantiation time, or on the first ->get_acl call.
e77819e57   Linus Torvalds   vfs: move ACL cac...
235
  	 *
4e34e719e   Christoph Hellwig   fs: take the ACL ...
236
237
  	 * If the filesystem doesn't have a get_acl() function at all, we'll
  	 * just create the negative cache entry.
e77819e57   Linus Torvalds   vfs: move ACL cac...
238
239
  	 */
  	if (acl == ACL_NOT_CACHED) {
4e34e719e   Christoph Hellwig   fs: take the ACL ...
240
241
242
243
244
245
246
247
  	        if (inode->i_op->get_acl) {
  			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
  			if (IS_ERR(acl))
  				return PTR_ERR(acl);
  		} else {
  		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
  		        return -EAGAIN;
  		}
e77819e57   Linus Torvalds   vfs: move ACL cac...
248
249
250
251
252
253
254
  	}
  
  	if (acl) {
  	        int error = posix_acl_permission(inode, acl, mask);
  	        posix_acl_release(acl);
  	        return error;
  	}
84635d68b   Linus Torvalds   vfs: fix check_ac...
255
  #endif
e77819e57   Linus Torvalds   vfs: move ACL cac...
256
257
258
  
  	return -EAGAIN;
  }
5909ccaa3   Linus Torvalds   Make 'check_acl()...
259
  /*
948409c74   Andreas Gruenbacher   vfs: add a commen...
260
   * This does the basic permission checking
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
   */
7e40145eb   Al Viro   ->permission() sa...
262
  static int acl_permission_check(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  {
26cf46be9   Linus Torvalds   vfs: micro-optimi...
264
  	unsigned int mode = inode->i_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265

8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
266
  	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
  		mode >>= 6;
  	else {
e77819e57   Linus Torvalds   vfs: move ACL cac...
269
  		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
7e40145eb   Al Viro   ->permission() sa...
270
  			int error = check_acl(inode, mask);
b74c79e99   Nick Piggin   fs: provide rcu-w...
271
272
  			if (error != -EAGAIN)
  				return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
275
276
277
278
279
280
281
  		}
  
  		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...
282
  	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
  		return 0;
5909ccaa3   Linus Torvalds   Make 'check_acl()...
284
285
286
287
  	return -EACCES;
  }
  
  /**
b74c79e99   Nick Piggin   fs: provide rcu-w...
288
   * generic_permission -  check for access rights on a Posix-like filesystem
5909ccaa3   Linus Torvalds   Make 'check_acl()...
289
   * @inode:	inode to check access rights for
8fd90c8d1   Andreas Gruenbacher   vfs: indicate tha...
290
   * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
5909ccaa3   Linus Torvalds   Make 'check_acl()...
291
292
293
294
   *
   * 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...
295
296
297
298
299
   * 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()...
300
   */
2830ba7f3   Al Viro   ->permission() sa...
301
  int generic_permission(struct inode *inode, int mask)
5909ccaa3   Linus Torvalds   Make 'check_acl()...
302
303
304
305
  {
  	int ret;
  
  	/*
948409c74   Andreas Gruenbacher   vfs: add a commen...
306
  	 * Do the basic permission checks.
5909ccaa3   Linus Torvalds   Make 'check_acl()...
307
  	 */
7e40145eb   Al Viro   ->permission() sa...
308
  	ret = acl_permission_check(inode, mask);
5909ccaa3   Linus Torvalds   Make 'check_acl()...
309
310
  	if (ret != -EACCES)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311

d594e7ec4   Al Viro   massage generic_p...
312
313
  	if (S_ISDIR(inode->i_mode)) {
  		/* DACs are overridable for directories */
1a48e2ac0   Eric W. Biederman   userns: Replace t...
314
  		if (inode_capable(inode, CAP_DAC_OVERRIDE))
d594e7ec4   Al Viro   massage generic_p...
315
316
  			return 0;
  		if (!(mask & MAY_WRITE))
1a48e2ac0   Eric W. Biederman   userns: Replace t...
317
  			if (inode_capable(inode, CAP_DAC_READ_SEARCH))
d594e7ec4   Al Viro   massage generic_p...
318
319
320
  				return 0;
  		return -EACCES;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
322
  	/*
  	 * Read/write DACs are always overridable.
d594e7ec4   Al Viro   massage generic_p...
323
324
  	 * Executable DACs are overridable when there is
  	 * at least one exec bit set.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
  	 */
d594e7ec4   Al Viro   massage generic_p...
326
  	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
1a48e2ac0   Eric W. Biederman   userns: Replace t...
327
  		if (inode_capable(inode, CAP_DAC_OVERRIDE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
332
  			return 0;
  
  	/*
  	 * Searching includes executable on directories, else just read.
  	 */
7ea660014   Serge E. Hallyn   generic_permissio...
333
  	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec4   Al Viro   massage generic_p...
334
  	if (mask == MAY_READ)
1a48e2ac0   Eric W. Biederman   userns: Replace t...
335
  		if (inode_capable(inode, CAP_DAC_READ_SEARCH))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
  			return 0;
  
  	return -EACCES;
  }
3ddcd0569   Linus Torvalds   vfs: optimize ino...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  /*
   * 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
359
  /**
0bdaea901   David Howells   VFS: Split inode_...
360
361
362
   * __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
363
   *
0bdaea901   David Howells   VFS: Split inode_...
364
   * Check for read/write/execute permissions on an inode.
948409c74   Andreas Gruenbacher   vfs: add a commen...
365
366
   *
   * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
0bdaea901   David Howells   VFS: Split inode_...
367
368
369
   *
   * This does not check for a read-only file system.  You probably want
   * inode_permission().
cb23beb55   Christoph Hellwig   kill vfs_permission
370
   */
0bdaea901   David Howells   VFS: Split inode_...
371
  int __inode_permission(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
  {
e6305c43e   Al Viro   [PATCH] sanitize ...
373
  	int retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374

3ddcd0569   Linus Torvalds   vfs: optimize ino...
375
  	if (unlikely(mask & MAY_WRITE)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
377
378
379
380
381
  		/*
  		 * Nobody gets write access to an immutable file.
  		 */
  		if (IS_IMMUTABLE(inode))
  			return -EACCES;
  	}
3ddcd0569   Linus Torvalds   vfs: optimize ino...
382
  	retval = do_inode_permission(inode, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
384
  	if (retval)
  		return retval;
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
385
386
387
  	retval = devcgroup_inode_permission(inode, mask);
  	if (retval)
  		return retval;
d09ca7397   Eric Paris   security: make LS...
388
  	return security_inode_permission(inode, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
389
  }
f4d6ff89d   Al Viro   move exec_permiss...
390
  /**
0bdaea901   David Howells   VFS: Split inode_...
391
392
   * sb_permission - Check superblock-level permissions
   * @sb: Superblock of inode to check permission on
55852635a   Randy Dunlap   fs: fix fs/namei....
393
   * @inode: Inode to check permission on
0bdaea901   David Howells   VFS: Split inode_...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
   * @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);
  }
  
  /**
5dd784d04   Jan Blunck   Introduce path_get()
433
434
435
436
437
   * 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...
438
  void path_get(const struct path *path)
5dd784d04   Jan Blunck   Introduce path_get()
439
440
441
442
443
444
445
  {
  	mntget(path->mnt);
  	dget(path->dentry);
  }
  EXPORT_SYMBOL(path_get);
  
  /**
1d957f9bf   Jan Blunck   Introduce path_put()
446
447
448
449
450
   * 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...
451
  void path_put(const struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
  {
1d957f9bf   Jan Blunck   Introduce path_put()
453
454
  	dput(path->dentry);
  	mntput(path->mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  }
1d957f9bf   Jan Blunck   Introduce path_put()
456
  EXPORT_SYMBOL(path_put);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
457

19660af73   Al Viro   consolidate namei...
458
  /*
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
459
   * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af73   Al Viro   consolidate namei...
460
461
462
463
464
465
466
   * 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 ...
467
   */
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
468

32a7991b6   Al Viro   tidy up namei.c a...
469
470
471
472
473
474
475
476
477
478
479
  static inline void lock_rcu_walk(void)
  {
  	br_read_lock(&vfsmount_lock);
  	rcu_read_lock();
  }
  
  static inline void unlock_rcu_walk(void)
  {
  	rcu_read_unlock();
  	br_read_unlock(&vfsmount_lock);
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
480
  /**
19660af73   Al Viro   consolidate namei...
481
482
483
   * unlazy_walk - try to switch to ref-walk mode.
   * @nd: nameidata pathwalk data
   * @dentry: child of nd->path.dentry or NULL
39191628e   Randy Dunlap   fs: fix namei.c k...
484
   * Returns: 0 on success, -ECHILD on failure
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
485
   *
19660af73   Al Viro   consolidate namei...
486
487
488
   * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
   * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
   * @nd or NULL.  Must be called from rcu-walk context.
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
489
   */
19660af73   Al Viro   consolidate namei...
490
  static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
491
492
493
  {
  	struct fs_struct *fs = current->fs;
  	struct dentry *parent = nd->path.dentry;
5b6ca027d   Al Viro   reduce vfs_path_l...
494
  	int want_root = 0;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
495
496
  
  	BUG_ON(!(nd->flags & LOOKUP_RCU));
5b6ca027d   Al Viro   reduce vfs_path_l...
497
498
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  		want_root = 1;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
499
500
501
502
503
504
  		spin_lock(&fs->lock);
  		if (nd->root.mnt != fs->root.mnt ||
  				nd->root.dentry != fs->root.dentry)
  			goto err_root;
  	}
  	spin_lock(&parent->d_lock);
19660af73   Al Viro   consolidate namei...
505
506
507
508
509
  	if (!dentry) {
  		if (!__d_rcu_to_refcount(parent, nd->seq))
  			goto err_parent;
  		BUG_ON(nd->inode != parent->d_inode);
  	} else {
94c0d4ecb   Al Viro   Fix ->d_lock lock...
510
511
  		if (dentry->d_parent != parent)
  			goto err_parent;
19660af73   Al Viro   consolidate namei...
512
513
514
515
516
517
518
519
520
521
522
523
524
525
  		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  		if (!__d_rcu_to_refcount(dentry, nd->seq))
  			goto err_child;
  		/*
  		 * If the sequence check on the child dentry passed, then
  		 * the child has not been removed from its parent. This
  		 * means the parent dentry must be valid and able to take
  		 * a reference at this point.
  		 */
  		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  		BUG_ON(!parent->d_count);
  		parent->d_count++;
  		spin_unlock(&dentry->d_lock);
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
526
  	spin_unlock(&parent->d_lock);
5b6ca027d   Al Viro   reduce vfs_path_l...
527
  	if (want_root) {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
528
529
530
531
  		path_get(&nd->root);
  		spin_unlock(&fs->lock);
  	}
  	mntget(nd->path.mnt);
32a7991b6   Al Viro   tidy up namei.c a...
532
  	unlock_rcu_walk();
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
533
534
  	nd->flags &= ~LOOKUP_RCU;
  	return 0;
19660af73   Al Viro   consolidate namei...
535
536
  
  err_child:
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
537
  	spin_unlock(&dentry->d_lock);
19660af73   Al Viro   consolidate namei...
538
  err_parent:
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
539
540
  	spin_unlock(&parent->d_lock);
  err_root:
5b6ca027d   Al Viro   reduce vfs_path_l...
541
  	if (want_root)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
542
543
544
  		spin_unlock(&fs->lock);
  	return -ECHILD;
  }
4ce16ef3f   Al Viro   fs/namei.c: don't...
545
  static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
34286d666   Nick Piggin   fs: rcu-walk awar...
546
  {
4ce16ef3f   Al Viro   fs/namei.c: don't...
547
  	return dentry->d_op->d_revalidate(dentry, flags);
34286d666   Nick Piggin   fs: rcu-walk awar...
548
  }
9f1fafee9   Al Viro   merge handle_reva...
549
550
551
  /**
   * complete_walk - successful completion of path walk
   * @nd:  pointer nameidata
39159de2a   Jeff Layton   vfs: force reval ...
552
   *
9f1fafee9   Al Viro   merge handle_reva...
553
554
555
556
557
   * 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 ...
558
   */
9f1fafee9   Al Viro   merge handle_reva...
559
  static int complete_walk(struct nameidata *nd)
39159de2a   Jeff Layton   vfs: force reval ...
560
  {
16c2cd717   Al Viro   untangle the "nee...
561
  	struct dentry *dentry = nd->path.dentry;
39159de2a   Jeff Layton   vfs: force reval ...
562
  	int status;
39159de2a   Jeff Layton   vfs: force reval ...
563

9f1fafee9   Al Viro   merge handle_reva...
564
565
566
567
568
569
570
  	if (nd->flags & LOOKUP_RCU) {
  		nd->flags &= ~LOOKUP_RCU;
  		if (!(nd->flags & LOOKUP_ROOT))
  			nd->root.mnt = NULL;
  		spin_lock(&dentry->d_lock);
  		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
  			spin_unlock(&dentry->d_lock);
32a7991b6   Al Viro   tidy up namei.c a...
571
  			unlock_rcu_walk();
9f1fafee9   Al Viro   merge handle_reva...
572
573
574
575
576
  			return -ECHILD;
  		}
  		BUG_ON(nd->inode != dentry->d_inode);
  		spin_unlock(&dentry->d_lock);
  		mntget(nd->path.mnt);
32a7991b6   Al Viro   tidy up namei.c a...
577
  		unlock_rcu_walk();
9f1fafee9   Al Viro   merge handle_reva...
578
  	}
16c2cd717   Al Viro   untangle the "nee...
579
580
  	if (likely(!(nd->flags & LOOKUP_JUMPED)))
  		return 0;
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
581
  	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
39159de2a   Jeff Layton   vfs: force reval ...
582
  		return 0;
ecf3d1f1a   Jeff Layton   vfs: kill FS_REVA...
583
  	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
39159de2a   Jeff Layton   vfs: force reval ...
584
585
  	if (status > 0)
  		return 0;
16c2cd717   Al Viro   untangle the "nee...
586
  	if (!status)
39159de2a   Jeff Layton   vfs: force reval ...
587
  		status = -ESTALE;
16c2cd717   Al Viro   untangle the "nee...
588

9f1fafee9   Al Viro   merge handle_reva...
589
  	path_put(&nd->path);
39159de2a   Jeff Layton   vfs: force reval ...
590
591
  	return status;
  }
2a7378711   Al Viro   Cache root in nam...
592
593
  static __always_inline void set_root(struct nameidata *nd)
  {
f7ad3c6be   Miklos Szeredi   vfs: add helpers ...
594
595
  	if (!nd->root.mnt)
  		get_fs_root(current->fs, &nd->root);
2a7378711   Al Viro   Cache root in nam...
596
  }
6de88d729   Al Viro   kill __link_path_...
597
  static int link_path_walk(const char *, struct nameidata *);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
598
599
600
601
  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...
602
603
604
605
606
  		unsigned seq;
  
  		do {
  			seq = read_seqcount_begin(&fs->seq);
  			nd->root = fs->root;
c1530019e   Tim Chen   vfs: Fix absolute...
607
  			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
c28cc3646   Nick Piggin   fs: fs_struct use...
608
  		} while (read_seqcount_retry(&fs->seq, seq));
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
609
610
  	}
  }
f16623569   Arjan van de Ven   [PATCH] Mark some...
611
  static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
613
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
614
615
616
617
  	if (IS_ERR(link))
  		goto fail;
  
  	if (*link == '/') {
2a7378711   Al Viro   Cache root in nam...
618
  		set_root(nd);
1d957f9bf   Jan Blunck   Introduce path_put()
619
  		path_put(&nd->path);
2a7378711   Al Viro   Cache root in nam...
620
621
  		nd->path = nd->root;
  		path_get(&nd->root);
16c2cd717   Al Viro   untangle the "nee...
622
  		nd->flags |= LOOKUP_JUMPED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
624
  	nd->inode = nd->path.dentry->d_inode;
b4091d5f6   Christoph Hellwig   kill walk_init_root
625

31e6b01f4   Nick Piggin   fs: rcu-walk for ...
626
627
  	ret = link_path_walk(link, nd);
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
628
  fail:
1d957f9bf   Jan Blunck   Introduce path_put()
629
  	path_put(&nd->path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
630
631
  	return PTR_ERR(link);
  }
1d957f9bf   Jan Blunck   Introduce path_put()
632
  static void path_put_conditional(struct path *path, struct nameidata *nd)
051d38125   Ian Kent   [PATCH] autofs4: ...
633
634
  {
  	dput(path->dentry);
4ac913785   Jan Blunck   Embed a struct pa...
635
  	if (path->mnt != nd->path.mnt)
051d38125   Ian Kent   [PATCH] autofs4: ...
636
637
  		mntput(path->mnt);
  }
7b9337aaf   Nick Piggin   fs: namei fix ->p...
638
639
  static inline void path_to_nameidata(const struct path *path,
  					struct nameidata *nd)
051d38125   Ian Kent   [PATCH] autofs4: ...
640
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
641
642
643
644
  	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 ...
645
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
646
  	nd->path.mnt = path->mnt;
4ac913785   Jan Blunck   Embed a struct pa...
647
  	nd->path.dentry = path->dentry;
051d38125   Ian Kent   [PATCH] autofs4: ...
648
  }
b5fb63c18   Christoph Hellwig   fs: add nd_jump_link
649
650
651
652
653
654
655
656
657
658
659
  /*
   * 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
660
  }
574197e0d   Al Viro   tidy the trailing...
661
662
663
  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...
664
  	if (inode->i_op->put_link)
574197e0d   Al Viro   tidy the trailing...
665
666
667
  		inode->i_op->put_link(link->dentry, nd, cookie);
  	path_put(link);
  }
561ec64ae   Linus Torvalds   VFS: don't do pro...
668
669
  int sysctl_protected_symlinks __read_mostly = 0;
  int sysctl_protected_hardlinks __read_mostly = 0;
800179c9b   Kees Cook   fs: add link rest...
670
671
672
673
  
  /**
   * may_follow_link - Check symlink following for unsafe situations
   * @link: The path of the symlink
55852635a   Randy Dunlap   fs: fix fs/namei....
674
   * @nd: nameidata pathwalk data
800179c9b   Kees Cook   fs: add link rest...
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
   *
   * 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...
697
  	if (uid_eq(current_cred()->fsuid, inode->i_uid))
800179c9b   Kees Cook   fs: add link rest...
698
699
700
701
702
703
704
705
  		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...
706
  	if (uid_eq(parent->i_uid, inode->i_uid))
800179c9b   Kees Cook   fs: add link rest...
707
  		return 0;
ffd8d101a   Sasha Levin   fs: prevent use a...
708
  	audit_log_link_denied("follow_link", link);
800179c9b   Kees Cook   fs: add link rest...
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
  	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...
775
  	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
800179c9b   Kees Cook   fs: add link rest...
776
777
  	    capable(CAP_FOWNER))
  		return 0;
a51d9eaa4   Kees Cook   fs: add link rest...
778
  	audit_log_link_denied("linkat", link);
800179c9b   Kees Cook   fs: add link rest...
779
780
  	return -EPERM;
  }
def4af30c   Al Viro   Get rid of symlin...
781
  static __always_inline int
574197e0d   Al Viro   tidy the trailing...
782
  follow_link(struct path *link, struct nameidata *nd, void **p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
783
  {
7b9337aaf   Nick Piggin   fs: namei fix ->p...
784
  	struct dentry *dentry = link->dentry;
6d7b5aaed   Al Viro   namei.c: let foll...
785
786
  	int error;
  	char *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787

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

36f3b4f69   Al Viro   pull security_ino...
799
  	error = security_inode_follow_link(link->dentry, nd);
6d7b5aaed   Al Viro   namei.c: let foll...
800
801
  	if (error)
  		goto out_put_nd_path;
36f3b4f69   Al Viro   pull security_ino...
802

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

962830df3   Andi Kleen   brlocks/lglocks: ...
854
  	br_read_lock(&vfsmount_lock);
0714a5338   Al Viro   vfs: now it can b...
855
  	parent = mnt->mnt_parent;
3c0a61636   Al Viro   unobfuscate follo...
856
  	if (parent == mnt) {
962830df3   Andi Kleen   brlocks/lglocks: ...
857
  		br_read_unlock(&vfsmount_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
858
859
  		return 0;
  	}
0714a5338   Al Viro   vfs: now it can b...
860
  	mntget(&parent->mnt);
a73324da7   Al Viro   vfs: move mnt_mou...
861
  	mountpoint = dget(mnt->mnt_mountpoint);
962830df3   Andi Kleen   brlocks/lglocks: ...
862
  	br_read_unlock(&vfsmount_lock);
bab77ebf5   Al Viro   switch follow_up(...
863
864
865
  	dput(path->dentry);
  	path->dentry = mountpoint;
  	mntput(path->mnt);
0714a5338   Al Viro   vfs: now it can b...
866
  	path->mnt = &parent->mnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867
868
  	return 1;
  }
b5c84bf6f   Nick Piggin   fs: dcache remove...
869
  /*
9875cf806   David Howells   Add a dentry op t...
870
871
872
   * 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
873
   */
9875cf806   David Howells   Add a dentry op t...
874
875
  static int follow_automount(struct path *path, unsigned flags,
  			    bool *need_mntput)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
876
  {
9875cf806   David Howells   Add a dentry op t...
877
  	struct vfsmount *mnt;
ea5b778a8   David Howells   Unexport do_add_m...
878
  	int err;
9875cf806   David Howells   Add a dentry op t...
879
880
881
  
  	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  		return -EREMOTE;
0ec26fd06   Miklos Szeredi   vfs: automount sh...
882
883
884
885
886
887
888
889
890
891
  	/* 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...
892
  	 */
0ec26fd06   Miklos Szeredi   vfs: automount sh...
893
  	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
d94c177be   Linus Torvalds   vfs pathname look...
894
  		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
0ec26fd06   Miklos Szeredi   vfs: automount sh...
895
896
  	    path->dentry->d_inode)
  		return -EISDIR;
9875cf806   David Howells   Add a dentry op t...
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
  	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
912
  		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9875cf806   David Howells   Add a dentry op t...
913
914
  			return -EREMOTE;
  		return PTR_ERR(mnt);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
915
  	}
ea5b778a8   David Howells   Unexport do_add_m...
916

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

8aef18845   Al Viro   VFS: Fix vfsmount...
920
921
922
923
924
  	if (!*need_mntput) {
  		/* lock_mount() may release path->mnt on error */
  		mntget(path->mnt);
  		*need_mntput = true;
  	}
19a167af7   Al Viro   Take the completi...
925
  	err = finish_automount(mnt, path);
9875cf806   David Howells   Add a dentry op t...
926

ea5b778a8   David Howells   Unexport do_add_m...
927
928
929
  	switch (err) {
  	case -EBUSY:
  		/* Someone else made a mount here whilst we were busy */
19a167af7   Al Viro   Take the completi...
930
  		return 0;
ea5b778a8   David Howells   Unexport do_add_m...
931
  	case 0:
8aef18845   Al Viro   VFS: Fix vfsmount...
932
  		path_put(path);
ea5b778a8   David Howells   Unexport do_add_m...
933
934
  		path->mnt = mnt;
  		path->dentry = dget(mnt->mnt_root);
ea5b778a8   David Howells   Unexport do_add_m...
935
  		return 0;
19a167af7   Al Viro   Take the completi...
936
937
  	default:
  		return err;
ea5b778a8   David Howells   Unexport do_add_m...
938
  	}
19a167af7   Al Viro   Take the completi...
939

463ffb2e9   Al Viro   [PATCH] namei fix...
940
  }
9875cf806   David Howells   Add a dentry op t...
941
942
  /*
   * Handle a dentry that is managed in some way.
cc53ce53c   David Howells   Add a dentry op t...
943
   * - Flagged for transit management (autofs)
9875cf806   David Howells   Add a dentry op t...
944
945
946
947
948
949
950
951
   * - 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
952
  {
8aef18845   Al Viro   VFS: Fix vfsmount...
953
  	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9875cf806   David Howells   Add a dentry op t...
954
955
  	unsigned managed;
  	bool need_mntput = false;
8aef18845   Al Viro   VFS: Fix vfsmount...
956
  	int ret = 0;
9875cf806   David Howells   Add a dentry op t...
957
958
959
960
961
962
963
  
  	/* 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...
964
965
966
967
968
  		/* 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...
969
  			ret = path->dentry->d_op->d_manage(path->dentry, false);
cc53ce53c   David Howells   Add a dentry op t...
970
  			if (ret < 0)
8aef18845   Al Viro   VFS: Fix vfsmount...
971
  				break;
cc53ce53c   David Howells   Add a dentry op t...
972
  		}
9875cf806   David Howells   Add a dentry op t...
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
  		/* 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
  			 * namespace got unmounted before we managed to get the
  			 * vfsmount_lock */
  		}
  
  		/* 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...
996
  				break;
9875cf806   David Howells   Add a dentry op t...
997
998
999
1000
1001
  			continue;
  		}
  
  		/* We didn't change the current path point */
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1002
  	}
8aef18845   Al Viro   VFS: Fix vfsmount...
1003
1004
1005
1006
1007
  
  	if (need_mntput && path->mnt == mnt)
  		mntput(path->mnt);
  	if (ret == -EISDIR)
  		ret = 0;
a3fbbde70   Al Viro   VFS: we need to s...
1008
  	return ret < 0 ? ret : need_mntput;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1009
  }
cc53ce53c   David Howells   Add a dentry op t...
1010
  int follow_down_one(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1011
1012
  {
  	struct vfsmount *mounted;
1c755af4d   Al Viro   switch lookup_mnt()
1013
  	mounted = lookup_mnt(path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1014
  	if (mounted) {
9393bd07c   Al Viro   switch follow_down()
1015
1016
1017
1018
  		dput(path->dentry);
  		mntput(path->mnt);
  		path->mnt = mounted;
  		path->dentry = dget(mounted->mnt_root);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1019
1020
1021
1022
  		return 1;
  	}
  	return 0;
  }
62a7375e5   Ian Kent   vfs - check non-m...
1023
1024
1025
1026
1027
  static inline bool managed_dentry_might_block(struct dentry *dentry)
  {
  	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
  		dentry->d_op->d_manage(dentry, true) < 0);
  }
9875cf806   David Howells   Add a dentry op t...
1028
  /*
287548e46   Al Viro   split __follow_mo...
1029
1030
   * 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...
1031
1032
   */
  static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
287548e46   Al Viro   split __follow_mo...
1033
  			       struct inode **inode)
9875cf806   David Howells   Add a dentry op t...
1034
  {
62a7375e5   Ian Kent   vfs - check non-m...
1035
  	for (;;) {
c71053659   Al Viro   vfs: spread struc...
1036
  		struct mount *mounted;
62a7375e5   Ian Kent   vfs - check non-m...
1037
1038
1039
1040
  		/*
  		 * Don't forget we might have a non-mountpoint managed dentry
  		 * that wants to block transit.
  		 */
287548e46   Al Viro   split __follow_mo...
1041
  		if (unlikely(managed_dentry_might_block(path->dentry)))
ab90911ff   David Howells   Allow d_manage() ...
1042
  			return false;
62a7375e5   Ian Kent   vfs - check non-m...
1043
1044
1045
  
  		if (!d_mountpoint(path->dentry))
  			break;
9875cf806   David Howells   Add a dentry op t...
1046
1047
1048
  		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  		if (!mounted)
  			break;
c71053659   Al Viro   vfs: spread struc...
1049
1050
  		path->mnt = &mounted->mnt;
  		path->dentry = mounted->mnt.mnt_root;
a3fbbde70   Al Viro   VFS: we need to s...
1051
  		nd->flags |= LOOKUP_JUMPED;
9875cf806   David Howells   Add a dentry op t...
1052
  		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
594302624   Linus Torvalds   vfs: fix race in ...
1053
1054
1055
1056
1057
1058
  		/*
  		 * 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...
1059
  	}
9875cf806   David Howells   Add a dentry op t...
1060
1061
  	return true;
  }
dea393761   Al Viro   Trim excessive ar...
1062
  static void follow_mount_rcu(struct nameidata *nd)
287548e46   Al Viro   split __follow_mo...
1063
  {
dea393761   Al Viro   Trim excessive ar...
1064
  	while (d_mountpoint(nd->path.dentry)) {
c71053659   Al Viro   vfs: spread struc...
1065
  		struct mount *mounted;
dea393761   Al Viro   Trim excessive ar...
1066
  		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
287548e46   Al Viro   split __follow_mo...
1067
1068
  		if (!mounted)
  			break;
c71053659   Al Viro   vfs: spread struc...
1069
1070
  		nd->path.mnt = &mounted->mnt;
  		nd->path.dentry = mounted->mnt.mnt_root;
dea393761   Al Viro   Trim excessive ar...
1071
  		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
287548e46   Al Viro   split __follow_mo...
1072
1073
  	}
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1074
1075
  static int follow_dotdot_rcu(struct nameidata *nd)
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1076
  	set_root_rcu(nd);
9875cf806   David Howells   Add a dentry op t...
1077
  	while (1) {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
  		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(...
1089
  				goto failed;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1090
1091
1092
1093
1094
1095
1096
  			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 ...
1097
  	}
dea393761   Al Viro   Trim excessive ar...
1098
1099
  	follow_mount_rcu(nd);
  	nd->inode = nd->path.dentry->d_inode;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1100
  	return 0;
ef7562d52   Al Viro   make handle_dots(...
1101
1102
1103
  
  failed:
  	nd->flags &= ~LOOKUP_RCU;
5b6ca027d   Al Viro   reduce vfs_path_l...
1104
1105
  	if (!(nd->flags & LOOKUP_ROOT))
  		nd->root.mnt = NULL;
32a7991b6   Al Viro   tidy up namei.c a...
1106
  	unlock_rcu_walk();
ef7562d52   Al Viro   make handle_dots(...
1107
  	return -ECHILD;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1108
  }
9875cf806   David Howells   Add a dentry op t...
1109
  /*
cc53ce53c   David Howells   Add a dentry op t...
1110
1111
1112
   * 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...
1113
   */
7cc90cc3f   Al Viro   don't pass 'mount...
1114
  int follow_down(struct path *path)
cc53ce53c   David Howells   Add a dentry op t...
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
  {
  	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() ...
1134
  			ret = path->dentry->d_op->d_manage(
1aed3e420   Al Viro   lose 'mounting_he...
1135
  				path->dentry, false);
cc53ce53c   David Howells   Add a dentry op t...
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
  			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;
  }
  
  /*
9875cf806   David Howells   Add a dentry op t...
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
   * 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 ...
1173
  static void follow_dotdot(struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1174
  {
2a7378711   Al Viro   Cache root in nam...
1175
  	set_root(nd);
e518ddb7b   Andreas Mohr   [PATCH] fs/namei....
1176

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

2a7378711   Al Viro   Cache root in nam...
1180
1181
  		if (nd->path.dentry == nd->root.dentry &&
  		    nd->path.mnt == nd->root.mnt) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1182
1183
  			break;
  		}
4ac913785   Jan Blunck   Embed a struct pa...
1184
  		if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd708   Al Viro   Clean follow_dotd...
1185
1186
  			/* rare case of legitimate dget_parent()... */
  			nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1187
1188
1189
  			dput(old);
  			break;
  		}
3088dd708   Al Viro   Clean follow_dotd...
1190
  		if (!follow_up(&nd->path))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1191
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1192
  	}
79ed02261   Al Viro   switch follow_mou...
1193
  	follow_mount(&nd->path);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1194
  	nd->inode = nd->path.dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1195
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1196
  /*
bad611897   Miklos Szeredi   vfs: split __look...
1197
1198
1199
1200
1201
   * 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...
1202
   */
bad611897   Miklos Szeredi   vfs: split __look...
1203
  static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
201f956e4   Al Viro   fs/namei.c: don't...
1204
  				    unsigned int flags, bool *need_lookup)
baa038907   Nick Piggin   fs: dentry alloca...
1205
  {
baa038907   Nick Piggin   fs: dentry alloca...
1206
  	struct dentry *dentry;
bad611897   Miklos Szeredi   vfs: split __look...
1207
  	int error;
baa038907   Nick Piggin   fs: dentry alloca...
1208

bad611897   Miklos Szeredi   vfs: split __look...
1209
1210
1211
  	*need_lookup = false;
  	dentry = d_lookup(dir, name);
  	if (dentry) {
39e3c9553   Jeff Layton   vfs: remove DCACH...
1212
  		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
201f956e4   Al Viro   fs/namei.c: don't...
1213
  			error = d_revalidate(dentry, flags);
bad611897   Miklos Szeredi   vfs: split __look...
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
  			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...
1225

bad611897   Miklos Szeredi   vfs: split __look...
1226
1227
1228
1229
  	if (!dentry) {
  		dentry = d_alloc(dir, name);
  		if (unlikely(!dentry))
  			return ERR_PTR(-ENOMEM);
baa038907   Nick Piggin   fs: dentry alloca...
1230

bad611897   Miklos Szeredi   vfs: split __look...
1231
  		*need_lookup = true;
baa038907   Nick Piggin   fs: dentry alloca...
1232
1233
1234
1235
1236
  	}
  	return dentry;
  }
  
  /*
bad611897   Miklos Szeredi   vfs: split __look...
1237
1238
1239
1240
   * Call i_op->lookup on the dentry.  The dentry must be negative but may be
   * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
   *
   * dir->d_inode->i_mutex must be held
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1241
   */
bad611897   Miklos Szeredi   vfs: split __look...
1242
  static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
72bd866a0   Al Viro   fs/namei.c: don't...
1243
  				  unsigned int flags)
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1244
  {
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1245
1246
1247
  	struct dentry *old;
  
  	/* Don't create child dentry for a dead directory. */
bad611897   Miklos Szeredi   vfs: split __look...
1248
  	if (unlikely(IS_DEADDIR(dir))) {
e188dc02d   Miklos Szeredi   vfs: fix d_inode_...
1249
  		dput(dentry);
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1250
  		return ERR_PTR(-ENOENT);
e188dc02d   Miklos Szeredi   vfs: fix d_inode_...
1251
  	}
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1252

72bd866a0   Al Viro   fs/namei.c: don't...
1253
  	old = dir->i_op->lookup(dir, dentry, flags);
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1254
1255
1256
1257
1258
1259
  	if (unlikely(old)) {
  		dput(dentry);
  		dentry = old;
  	}
  	return dentry;
  }
a32555466   Al Viro   untangling do_loo...
1260
  static struct dentry *__lookup_hash(struct qstr *name,
72bd866a0   Al Viro   fs/namei.c: don't...
1261
  		struct dentry *base, unsigned int flags)
a32555466   Al Viro   untangling do_loo...
1262
  {
bad611897   Miklos Szeredi   vfs: split __look...
1263
  	bool need_lookup;
a32555466   Al Viro   untangling do_loo...
1264
  	struct dentry *dentry;
72bd866a0   Al Viro   fs/namei.c: don't...
1265
  	dentry = lookup_dcache(name, base, flags, &need_lookup);
bad611897   Miklos Szeredi   vfs: split __look...
1266
1267
  	if (!need_lookup)
  		return dentry;
a32555466   Al Viro   untangling do_loo...
1268

72bd866a0   Al Viro   fs/namei.c: don't...
1269
  	return lookup_real(base->d_inode, dentry, flags);
a32555466   Al Viro   untangling do_loo...
1270
  }
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1271
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1272
1273
1274
1275
   *  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 ...
1276
  static int lookup_fast(struct nameidata *nd,
697f514df   Miklos Szeredi   vfs: split do_loo...
1277
  		       struct path *path, struct inode **inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1278
  {
4ac913785   Jan Blunck   Embed a struct pa...
1279
  	struct vfsmount *mnt = nd->path.mnt;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1280
  	struct dentry *dentry, *parent = nd->path.dentry;
5a18fff20   Al Viro   untangle do_lookup()
1281
1282
  	int need_reval = 1;
  	int status = 1;
9875cf806   David Howells   Add a dentry op t...
1283
  	int err;
3cac260ad   Al Viro   Take hash recalcu...
1284
  	/*
b04f784e5   Nick Piggin   fs: remove extra ...
1285
1286
1287
1288
  	 * 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 ...
1289
1290
  	if (nd->flags & LOOKUP_RCU) {
  		unsigned seq;
e97cdc87b   Al Viro   lookup_fast: get ...
1291
  		dentry = __d_lookup_rcu(parent, &nd->last, &seq, nd->inode);
5a18fff20   Al Viro   untangle do_lookup()
1292
1293
  		if (!dentry)
  			goto unlazy;
12f8ad4b0   Linus Torvalds   vfs: clean up __d...
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
  		/*
  		 * 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 ...
1309
1310
  		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  			return -ECHILD;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1311
  		nd->seq = seq;
5a18fff20   Al Viro   untangle do_lookup()
1312

24643087e   Al Viro   in do_lookup() sp...
1313
  		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
4ce16ef3f   Al Viro   fs/namei.c: don't...
1314
  			status = d_revalidate(dentry, nd->flags);
5a18fff20   Al Viro   untangle do_lookup()
1315
1316
1317
1318
1319
  			if (unlikely(status <= 0)) {
  				if (status != -ECHILD)
  					need_reval = 0;
  				goto unlazy;
  			}
24643087e   Al Viro   in do_lookup() sp...
1320
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1321
1322
  		path->mnt = mnt;
  		path->dentry = dentry;
d6e9bd256   Al Viro   Lift the check fo...
1323
1324
1325
1326
1327
  		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
  			goto unlazy;
  		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  			goto unlazy;
  		return 0;
5a18fff20   Al Viro   untangle do_lookup()
1328
  unlazy:
19660af73   Al Viro   consolidate namei...
1329
1330
  		if (unlazy_walk(nd, dentry))
  			return -ECHILD;
5a18fff20   Al Viro   untangle do_lookup()
1331
  	} else {
e97cdc87b   Al Viro   lookup_fast: get ...
1332
  		dentry = __d_lookup(parent, &nd->last);
9875cf806   David Howells   Add a dentry op t...
1333
  	}
5a18fff20   Al Viro   untangle do_lookup()
1334

81e6f5208   Al Viro   untangling do_loo...
1335
1336
  	if (unlikely(!dentry))
  		goto need_lookup;
5a18fff20   Al Viro   untangle do_lookup()
1337
  	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
4ce16ef3f   Al Viro   fs/namei.c: don't...
1338
  		status = d_revalidate(dentry, nd->flags);
5a18fff20   Al Viro   untangle do_lookup()
1339
1340
1341
1342
1343
1344
1345
  	if (unlikely(status <= 0)) {
  		if (status < 0) {
  			dput(dentry);
  			return status;
  		}
  		if (!d_invalidate(dentry)) {
  			dput(dentry);
81e6f5208   Al Viro   untangling do_loo...
1346
  			goto need_lookup;
5a18fff20   Al Viro   untangle do_lookup()
1347
  		}
24643087e   Al Viro   in do_lookup() sp...
1348
  	}
697f514df   Miklos Szeredi   vfs: split do_loo...
1349

9875cf806   David Howells   Add a dentry op t...
1350
1351
1352
  	path->mnt = mnt;
  	path->dentry = dentry;
  	err = follow_managed(path, nd->flags);
893122141   Ian Kent   vfs - fix dentry ...
1353
1354
  	if (unlikely(err < 0)) {
  		path_put_conditional(path, nd);
9875cf806   David Howells   Add a dentry op t...
1355
  		return err;
893122141   Ian Kent   vfs - fix dentry ...
1356
  	}
a3fbbde70   Al Viro   VFS: we need to s...
1357
1358
  	if (err)
  		nd->flags |= LOOKUP_JUMPED;
9875cf806   David Howells   Add a dentry op t...
1359
  	*inode = path->dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1360
  	return 0;
81e6f5208   Al Viro   untangling do_loo...
1361
1362
  
  need_lookup:
697f514df   Miklos Szeredi   vfs: split do_loo...
1363
1364
1365
1366
  	return 1;
  }
  
  /* Fast lookup failed, do it the slow way */
cc2a52711   Al Viro   lookup_slow: get ...
1367
  static int lookup_slow(struct nameidata *nd, struct path *path)
697f514df   Miklos Szeredi   vfs: split do_loo...
1368
1369
1370
1371
1372
  {
  	struct dentry *dentry, *parent;
  	int err;
  
  	parent = nd->path.dentry;
81e6f5208   Al Viro   untangling do_loo...
1373
1374
1375
  	BUG_ON(nd->inode != parent->d_inode);
  
  	mutex_lock(&parent->d_inode->i_mutex);
cc2a52711   Al Viro   lookup_slow: get ...
1376
  	dentry = __lookup_hash(&nd->last, parent, nd->flags);
81e6f5208   Al Viro   untangling do_loo...
1377
1378
1379
  	mutex_unlock(&parent->d_inode->i_mutex);
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
697f514df   Miklos Szeredi   vfs: split do_loo...
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
  	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
1390
  }
52094c8a0   Al Viro   take RCU-dependen...
1391
1392
1393
  static inline int may_lookup(struct nameidata *nd)
  {
  	if (nd->flags & LOOKUP_RCU) {
4ad5abb3d   Al Viro   no reason to keep...
1394
  		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a0   Al Viro   take RCU-dependen...
1395
1396
  		if (err != -ECHILD)
  			return err;
19660af73   Al Viro   consolidate namei...
1397
  		if (unlazy_walk(nd, NULL))
52094c8a0   Al Viro   take RCU-dependen...
1398
1399
  			return -ECHILD;
  	}
4ad5abb3d   Al Viro   no reason to keep...
1400
  	return inode_permission(nd->inode, MAY_EXEC);
52094c8a0   Al Viro   take RCU-dependen...
1401
  }
9856fa1b2   Al Viro   pull handling of ...
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
  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...
1413
1414
1415
1416
1417
1418
  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...
1419
1420
  		if (!(nd->flags & LOOKUP_ROOT))
  			nd->root.mnt = NULL;
32a7991b6   Al Viro   tidy up namei.c a...
1421
  		unlock_rcu_walk();
951361f95   Al Viro   get rid of the la...
1422
1423
  	}
  }
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1424
1425
1426
1427
1428
1429
  /*
   * 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.
   */
7813b94a5   Linus Torvalds   vfs: rename 'do_f...
1430
  static inline int should_follow_link(struct inode *inode, int follow)
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
  {
  	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
  		if (likely(inode->i_op->follow_link))
  			return follow;
  
  		/* This gets set once for the inode lifetime */
  		spin_lock(&inode->i_lock);
  		inode->i_opflags |= IOP_NOFOLLOW;
  		spin_unlock(&inode->i_lock);
  	}
  	return 0;
  }
ce57dfc17   Al Viro   pull handling of ...
1443
  static inline int walk_component(struct nameidata *nd, struct path *path,
21b9b0739   Al Viro   get rid of name a...
1444
  		int follow)
ce57dfc17   Al Viro   pull handling of ...
1445
1446
1447
1448
1449
1450
1451
1452
  {
  	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...
1453
1454
  	if (unlikely(nd->last_type != LAST_NORM))
  		return handle_dots(nd, nd->last_type);
e97cdc87b   Al Viro   lookup_fast: get ...
1455
  	err = lookup_fast(nd, path, &inode);
ce57dfc17   Al Viro   pull handling of ...
1456
  	if (unlikely(err)) {
697f514df   Miklos Szeredi   vfs: split do_loo...
1457
1458
  		if (err < 0)
  			goto out_err;
cc2a52711   Al Viro   lookup_slow: get ...
1459
  		err = lookup_slow(nd, path);
697f514df   Miklos Szeredi   vfs: split do_loo...
1460
1461
1462
1463
  		if (err < 0)
  			goto out_err;
  
  		inode = path->dentry->d_inode;
ce57dfc17   Al Viro   pull handling of ...
1464
  	}
697f514df   Miklos Szeredi   vfs: split do_loo...
1465
1466
1467
  	err = -ENOENT;
  	if (!inode)
  		goto out_path_put;
7813b94a5   Linus Torvalds   vfs: rename 'do_f...
1468
  	if (should_follow_link(inode, follow)) {
19660af73   Al Viro   consolidate namei...
1469
1470
  		if (nd->flags & LOOKUP_RCU) {
  			if (unlikely(unlazy_walk(nd, path->dentry))) {
697f514df   Miklos Szeredi   vfs: split do_loo...
1471
1472
  				err = -ECHILD;
  				goto out_err;
19660af73   Al Viro   consolidate namei...
1473
1474
  			}
  		}
ce57dfc17   Al Viro   pull handling of ...
1475
1476
1477
1478
1479
1480
  		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...
1481
1482
1483
1484
1485
1486
  
  out_path_put:
  	path_to_nameidata(path, nd);
  out_err:
  	terminate_walk(nd);
  	return err;
ce57dfc17   Al Viro   pull handling of ...
1487
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1488
  /*
b356379a0   Al Viro   Turn resolution o...
1489
1490
1491
1492
1493
1494
1495
1496
1497
   * 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...
1498
1499
1500
1501
1502
  	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 ...
1503
  	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
b356379a0   Al Viro   Turn resolution o...
1504
1505
1506
1507
1508
1509
1510
  
  	nd->depth++;
  	current->link_count++;
  
  	do {
  		struct path link = *path;
  		void *cookie;
574197e0d   Al Viro   tidy the trailing...
1511
1512
  
  		res = follow_link(&link, nd, &cookie);
6d7b5aaed   Al Viro   namei.c: let foll...
1513
1514
  		if (res)
  			break;
21b9b0739   Al Viro   get rid of name a...
1515
  		res = walk_component(nd, path, LOOKUP_FOLLOW);
574197e0d   Al Viro   tidy the trailing...
1516
  		put_link(nd, &link, cookie);
b356379a0   Al Viro   Turn resolution o...
1517
1518
1519
1520
1521
1522
1523
1524
  	} while (res > 0);
  
  	current->link_count--;
  	nd->depth--;
  	return res;
  }
  
  /*
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
   * We really don't want to look at inode->i_op->lookup
   * when we don't have to. So we keep a cache bit in
   * the inode ->i_opflags field that says "yes, we can
   * do lookup on this inode".
   */
  static inline int can_lookup(struct inode *inode)
  {
  	if (likely(inode->i_opflags & IOP_LOOKUP))
  		return 1;
  	if (likely(!inode->i_op->lookup))
  		return 0;
  
  	/* We do this once for the lifetime of the inode */
  	spin_lock(&inode->i_lock);
  	inode->i_opflags |= IOP_LOOKUP;
  	spin_unlock(&inode->i_lock);
  	return 1;
  }
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
  /*
   * 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.
   *
   * - Little-endian machines (so that we can generate the mask
   *   of low bytes efficiently). Again, we *could* do a byte
   *   swapping load on big-endian architectures if that is not
   *   expensive enough to make the optimization worthless.
   *
   * - 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...
1566
  #include <asm/word-at-a-time.h>
bfcfaa77b   Linus Torvalds   vfs: use 'unsigne...
1567

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1678
1679
  	/* At this point we know we have a real path component. */
  	for(;;) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1680
  		struct qstr this;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1681
  		long len;
fe479a580   Al Viro   merge component t...
1682
  		int type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1683

52094c8a0   Al Viro   take RCU-dependen...
1684
  		err = may_lookup(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1685
1686
   		if (err)
  			break;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1687
  		len = hash_name(name, &this.hash);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1688
  		this.name = name;
200e9ef7a   Linus Torvalds   vfs: split up nam...
1689
  		this.len = len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1690

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

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

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

ce57dfc17   Al Viro   pull handling of ...
1732
  		if (err) {
b356379a0   Al Viro   Turn resolution o...
1733
  			err = nested_symlink(&next, nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1734
  			if (err)
a7472baba   Al Viro   make nameidata_de...
1735
  				return err;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1736
  		}
5f4a6a695   Al Viro   link_path_walk():...
1737
1738
1739
1740
  		if (!can_lookup(nd->inode)) {
  			err = -ENOTDIR; 
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1741
  	}
951361f95   Al Viro   get rid of the la...
1742
  	terminate_walk(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1743
1744
  	return err;
  }
70e9b3571   Al Viro   get rid of nd->file
1745
1746
  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 ...
1747
1748
  {
  	int retval = 0;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1749
1750
  
  	nd->last_type = LAST_ROOT; /* if there are only slashes... */
16c2cd717   Al Viro   untangle the "nee...
1751
  	nd->flags = flags | LOOKUP_JUMPED;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1752
  	nd->depth = 0;
5b6ca027d   Al Viro   reduce vfs_path_l...
1753
1754
  	if (flags & LOOKUP_ROOT) {
  		struct inode *inode = nd->root.dentry->d_inode;
73d049a40   Al Viro   open-style analog...
1755
  		if (*name) {
741b7c3f7   Al Viro   path_init(): make...
1756
  			if (!can_lookup(inode))
73d049a40   Al Viro   open-style analog...
1757
1758
1759
1760
1761
  				return -ENOTDIR;
  			retval = inode_permission(inode, MAY_EXEC);
  			if (retval)
  				return retval;
  		}
5b6ca027d   Al Viro   reduce vfs_path_l...
1762
1763
1764
  		nd->path = nd->root;
  		nd->inode = inode;
  		if (flags & LOOKUP_RCU) {
32a7991b6   Al Viro   tidy up namei.c a...
1765
  			lock_rcu_walk();
5b6ca027d   Al Viro   reduce vfs_path_l...
1766
1767
1768
1769
1770
1771
  			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  		} else {
  			path_get(&nd->path);
  		}
  		return 0;
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1772
  	nd->root.mnt = NULL;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1773
1774
  
  	if (*name=='/') {
e41f7d4ee   Al Viro   merge path_init a...
1775
  		if (flags & LOOKUP_RCU) {
32a7991b6   Al Viro   tidy up namei.c a...
1776
  			lock_rcu_walk();
e41f7d4ee   Al Viro   merge path_init a...
1777
1778
1779
1780
1781
1782
  			set_root_rcu(nd);
  		} else {
  			set_root(nd);
  			path_get(&nd->root);
  		}
  		nd->path = nd->root;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1783
  	} else if (dfd == AT_FDCWD) {
e41f7d4ee   Al Viro   merge path_init a...
1784
1785
1786
  		if (flags & LOOKUP_RCU) {
  			struct fs_struct *fs = current->fs;
  			unsigned seq;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1787

32a7991b6   Al Viro   tidy up namei.c a...
1788
  			lock_rcu_walk();
c28cc3646   Nick Piggin   fs: fs_struct use...
1789

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

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

f52e0c113   Al Viro   New AT_... flag: ...
1807
  		if (*name) {
741b7c3f7   Al Viro   path_init(): make...
1808
  			if (!can_lookup(dentry->d_inode)) {
2903ff019   Al Viro   switch simple cas...
1809
1810
1811
  				fdput(f);
  				return -ENOTDIR;
  			}
f52e0c113   Al Viro   New AT_... flag: ...
1812
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1813

2903ff019   Al Viro   switch simple cas...
1814
  		nd->path = f.file->f_path;
e41f7d4ee   Al Viro   merge path_init a...
1815
  		if (flags & LOOKUP_RCU) {
2903ff019   Al Viro   switch simple cas...
1816
1817
  			if (f.need_put)
  				*fp = f.file;
e41f7d4ee   Al Viro   merge path_init a...
1818
  			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
32a7991b6   Al Viro   tidy up namei.c a...
1819
  			lock_rcu_walk();
e41f7d4ee   Al Viro   merge path_init a...
1820
  		} else {
2903ff019   Al Viro   switch simple cas...
1821
1822
  			path_get(&nd->path);
  			fdput(f);
e41f7d4ee   Al Viro   merge path_init a...
1823
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1824
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1825

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

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

9f1fafee9   Al Viro   merge handle_reva...
1884
1885
  	if (!err)
  		err = complete_walk(nd);
bd92d7fed   Al Viro   Make trailing sym...
1886
1887
1888
1889
  
  	if (!err && nd->flags & LOOKUP_DIRECTORY) {
  		if (!nd->inode->i_op->lookup) {
  			path_put(&nd->path);
bd23a539d   Al Viro   fix leaks in path...
1890
  			err = -ENOTDIR;
bd92d7fed   Al Viro   Make trailing sym...
1891
1892
  		}
  	}
16c2cd717   Al Viro   untangle the "nee...
1893

70e9b3571   Al Viro   get rid of nd->file
1894
1895
  	if (base)
  		fput(base);
ee0827cd6   Al Viro   sanitize path_wal...
1896

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

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

f78570dd6   Jeff Layton   audit: remove unn...
1914
  	if (likely(!retval))
adb5c2473   Jeff Layton   audit: make audit...
1915
  		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
170aa3d02   Ulrich Drepper   [PATCH] namei.c: ...
1916
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1917
  }
873f1eedc   Jeff Layton   vfs: turn do_path...
1918
1919
1920
1921
1922
1923
1924
  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...
1925
1926
  /* 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 ...
1927
  {
79714f72d   Al Viro   get rid of kern_p...
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
  	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...
1938
  	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
79714f72d   Al Viro   get rid of kern_p...
1939
1940
1941
1942
1943
1944
1945
  	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 ...
1946
  }
d18114657   Al Viro   [PATCH] new helpe...
1947
1948
1949
1950
1951
1952
1953
1954
  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;
  }
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1955
1956
1957
1958
1959
1960
  /**
   * 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...
1961
   * @path: pointer to struct path to fill
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1962
1963
1964
   */
  int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  		    const char *name, unsigned int flags,
e0a012493   Al Viro   switch vfs_path_l...
1965
  		    struct path *path)
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1966
  {
e0a012493   Al Viro   switch vfs_path_l...
1967
1968
1969
1970
1971
  	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...
1972
  	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
e0a012493   Al Viro   switch vfs_path_l...
1973
1974
1975
1976
  	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...
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
2024
2025
2026
2027
  	/*
  	 * See if the low-level filesystem might want
  	 * to use its own hash..
  	 */
  	if (base->d_flags & DCACHE_OP_HASH) {
  		int err = base->d_op->d_hash(base, base->d_inode, &this);
  		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
  }
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
2034
2035
  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
2036
  {
2d8f30380   Al Viro   [PATCH] sanitize ...
2037
  	struct nameidata nd;
91a27b2a7   Jeff Layton   vfs: define struc...
2038
  	struct filename *tmp = getname_flags(name, flags, empty);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2039
  	int err = PTR_ERR(tmp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2040
  	if (!IS_ERR(tmp)) {
2d8f30380   Al Viro   [PATCH] sanitize ...
2041
2042
  
  		BUG_ON(flags & LOOKUP_PARENT);
873f1eedc   Jeff Layton   vfs: turn do_path...
2043
  		err = filename_lookup(dfd, tmp, flags, &nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2044
  		putname(tmp);
2d8f30380   Al Viro   [PATCH] sanitize ...
2045
2046
  		if (!err)
  			*path = nd.path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2047
2048
2049
  	}
  	return err;
  }
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
2050
2051
2052
  int user_path_at(int dfd, const char __user *name, unsigned flags,
  		 struct path *path)
  {
f7493e5d9   Linus Torvalds   vfs: tidy up spar...
2053
  	return user_path_at_empty(dfd, name, flags, path, NULL);
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
2054
  }
873f1eedc   Jeff Layton   vfs: turn do_path...
2055
2056
2057
2058
2059
2060
  /*
   * 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...
2061
  static struct filename *
9e790bd65   Jeff Layton   vfs: add a flags ...
2062
2063
  user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
  		 unsigned int flags)
2ad94ae65   Al Viro   [PATCH] new (loca...
2064
  {
91a27b2a7   Jeff Layton   vfs: define struc...
2065
  	struct filename *s = getname(path);
2ad94ae65   Al Viro   [PATCH] new (loca...
2066
  	int error;
9e790bd65   Jeff Layton   vfs: add a flags ...
2067
2068
  	/* only LOOKUP_REVAL is allowed in extra flags */
  	flags &= LOOKUP_REVAL;
2ad94ae65   Al Viro   [PATCH] new (loca...
2069
  	if (IS_ERR(s))
91a27b2a7   Jeff Layton   vfs: define struc...
2070
  		return s;
2ad94ae65   Al Viro   [PATCH] new (loca...
2071

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

91a27b2a7   Jeff Layton   vfs: define struc...
2078
  	return s;
2ad94ae65   Al Viro   [PATCH] new (loca...
2079
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2080
2081
2082
2083
2084
2085
  /*
   * 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...
2086
  	kuid_t fsuid = current_fsuid();
da9592ede   David Howells   CRED: Wrap task c...
2087

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2088
2089
  	if (!(dir->i_mode & S_ISVTX))
  		return 0;
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
2090
  	if (uid_eq(inode->i_uid, fsuid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2091
  		return 0;
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
2092
  	if (uid_eq(dir->i_uid, fsuid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2093
  		return 0;
1a48e2ac0   Eric W. Biederman   userns: Replace t...
2094
  	return !inode_capable(inode, CAP_FOWNER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
  }
  
  /*
   *	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().
   */
858119e15   Arjan van de Ven   [PATCH] Unlinline...
2116
  static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2117
2118
2119
2120
2121
2122
2123
  {
  	int error;
  
  	if (!victim->d_inode)
  		return -ENOENT;
  
  	BUG_ON(victim->d_parent->d_inode != dir);
4fa6b5ecb   Jeff Layton   audit: overhaul _...
2124
  	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2125

f419a2e3b   Al Viro   [PATCH] kill name...
2126
  	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2127
2128
2129
2130
2131
  	if (error)
  		return error;
  	if (IS_APPEND(dir))
  		return -EPERM;
  	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
f9454548e   Hugh Dickins   don't unlink an a...
2132
  	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
  		return -EPERM;
  	if (isdir) {
  		if (!S_ISDIR(victim->d_inode->i_mode))
  			return -ENOTDIR;
  		if (IS_ROOT(victim))
  			return -EBUSY;
  	} else if (S_ISDIR(victim->d_inode->i_mode))
  		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: ...
2156
  static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2157
2158
2159
2160
2161
  {
  	if (child->d_inode)
  		return -EEXIST;
  	if (IS_DEADDIR(dir))
  		return -ENOENT;
f419a2e3b   Al Viro   [PATCH] kill name...
2162
  	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2163
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2164
2165
2166
2167
2168
2169
2170
2171
  /*
   * 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: ...
2172
  		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2173
2174
  		return NULL;
  	}
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
2175
  	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2176

e2761a116   OGAWA Hirofumi   [PATCH vfs-2.6 2/...
2177
2178
2179
2180
2181
  	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
2182
  	}
e2761a116   OGAWA Hirofumi   [PATCH vfs-2.6 2/...
2183
2184
2185
2186
2187
  	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
2188
  	}
f2eace23e   Ingo Molnar   [PATCH] lockdep: ...
2189
2190
  	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
2191
2192
2193
2194
2195
  	return NULL;
  }
  
  void unlock_rename(struct dentry *p1, struct dentry *p2)
  {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2196
  	mutex_unlock(&p1->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2197
  	if (p1 != p2) {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2198
  		mutex_unlock(&p2->d_inode->i_mutex);
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
2199
  		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2200
2201
  	}
  }
4acdaf27e   Al Viro   switch ->create()...
2202
  int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
312b63fba   Al Viro   don't pass nameid...
2203
  		bool want_excl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2204
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2205
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2206
2207
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
2208
  	if (!dir->i_op->create)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2209
2210
2211
2212
2213
2214
  		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...
2215
  	error = dir->i_op->create(dir, dentry, mode, want_excl);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
2216
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
2217
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2218
2219
  	return error;
  }
73d049a40   Al Viro   open-style analog...
2220
  static int may_open(struct path *path, int acc_mode, int flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2221
  {
3fb64190a   Christoph Hellwig   pass a struct pat...
2222
  	struct dentry *dentry = path->dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2223
2224
  	struct inode *inode = dentry->d_inode;
  	int error;
bcda76524   Al Viro   Allow O_PATH for ...
2225
2226
2227
  	/* O_PATH? */
  	if (!acc_mode)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2228
2229
  	if (!inode)
  		return -ENOENT;
c8fe8f30c   Christoph Hellwig   cleanup may_open
2230
2231
  	switch (inode->i_mode & S_IFMT) {
  	case S_IFLNK:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2232
  		return -ELOOP;
c8fe8f30c   Christoph Hellwig   cleanup may_open
2233
2234
2235
2236
2237
2238
  	case S_IFDIR:
  		if (acc_mode & MAY_WRITE)
  			return -EISDIR;
  		break;
  	case S_IFBLK:
  	case S_IFCHR:
3fb64190a   Christoph Hellwig   pass a struct pat...
2239
  		if (path->mnt->mnt_flags & MNT_NODEV)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2240
  			return -EACCES;
c8fe8f30c   Christoph Hellwig   cleanup may_open
2241
2242
2243
  		/*FALLTHRU*/
  	case S_IFIFO:
  	case S_IFSOCK:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2244
  		flag &= ~O_TRUNC;
c8fe8f30c   Christoph Hellwig   cleanup may_open
2245
  		break;
4a3fd211c   Dave Hansen   [PATCH] r/o bind ...
2246
  	}
b41572e92   Dave Hansen   r/o bind mounts: ...
2247

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2252
2253
2254
2255
  	/*
  	 * An append-only file must be opened in append mode for writing.
  	 */
  	if (IS_APPEND(inode)) {
8737c9305   Al Viro   Switch may_open()...
2256
  		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b5212   Al Viro   O_TRUNC open shou...
2257
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2258
  		if (flag & O_TRUNC)
7715b5212   Al Viro   O_TRUNC open shou...
2259
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2260
2261
2262
  	}
  
  	/* O_NOATIME can only be set by the owner or superuser */
2e1496707   Serge E. Hallyn   userns: rename is...
2263
  	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b5212   Al Viro   O_TRUNC open shou...
2264
  		return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2265

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

e1181ee65   Jeff Layton   vfs: pass struct ...
2269
  static int handle_truncate(struct file *filp)
7715b5212   Al Viro   O_TRUNC open shou...
2270
  {
e1181ee65   Jeff Layton   vfs: pass struct ...
2271
  	struct path *path = &filp->f_path;
7715b5212   Al Viro   O_TRUNC open shou...
2272
2273
2274
2275
2276
2277
2278
2279
2280
  	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.
  	 */
  	error = locks_verify_locked(inode);
  	if (!error)
ea0d3ab23   Tetsuo Handa   LSM: Remove unuse...
2281
  		error = security_path_truncate(path);
7715b5212   Al Viro   O_TRUNC open shou...
2282
2283
2284
  	if (!error) {
  		error = do_truncate(path->dentry, 0,
  				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee65   Jeff Layton   vfs: pass struct ...
2285
  				    filp);
7715b5212   Al Viro   O_TRUNC open shou...
2286
2287
  	}
  	put_write_access(inode);
acd0c9351   Mimi Zohar   IMA: update ima_c...
2288
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2289
  }
d57999e15   Dave Hansen   [PATCH] do namei_...
2290
2291
  static inline int open_to_namei_flags(int flag)
  {
8a5e929dd   Al Viro   don't translitera...
2292
2293
  	if ((flag & O_ACCMODE) == 3)
  		flag--;
d57999e15   Dave Hansen   [PATCH] do namei_...
2294
2295
  	return flag;
  }
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
  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...
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
  /*
   * 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...
2321
2322
2323
  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...
2324
  			bool got_write, bool need_lookup,
2675a4eb6   Al Viro   fs/namei.c: get d...
2325
  			int *opened)
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2326
2327
2328
2329
2330
2331
  {
  	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...
2332
2333
2334
2335
2336
2337
2338
  	int create_error = 0;
  	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
  
  	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...
2339
  		error = -ENOENT;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2340
2341
  		goto out;
  	}
62b259d8b   Miklos Szeredi   vfs: atomic_open(...
2342
  	mode = op->mode;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2343
2344
  	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
  		mode &= ~current_umask();
f8310c592   Al Viro   fix O_EXCL handli...
2345
  	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2346
  		open_flag &= ~O_TRUNC;
47237687d   Al Viro   ->atomic_open() p...
2347
  		*opened |= FILE_CREATED;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
  	}
  
  	/*
  	 * 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...
2359
2360
2361
  	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...
2362
2363
2364
2365
2366
2367
2368
  			/*
  			 * 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...
2369
  			create_error = -EROFS;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2370
2371
2372
  			goto no_open;
  		} else {
  			/* No side effects, safe to clear O_CREAT */
64894cf84   Al Viro   simplify lookup_o...
2373
  			create_error = -EROFS;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2374
2375
2376
2377
2378
  			open_flag &= ~O_CREAT;
  		}
  	}
  
  	if (open_flag & O_CREAT) {
38227f78a   Miklos Szeredi   vfs: pass right c...
2379
  		error = may_o_create(&nd->path, dentry, mode);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
  		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
2390
2391
2392
  	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...
2393
  				      opened);
d95852777   Al Viro   make ->atomic_ope...
2394
  	if (error < 0) {
d95852777   Al Viro   make ->atomic_ope...
2395
2396
  		if (create_error && error == -ENOENT)
  			error = create_error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2397
2398
2399
2400
  		goto out;
  	}
  
  	acc_mode = op->acc_mode;
47237687d   Al Viro   ->atomic_open() p...
2401
  	if (*opened & FILE_CREATED) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2402
2403
2404
  		fsnotify_create(dir, dentry);
  		acc_mode = MAY_OPEN;
  	}
d95852777   Al Viro   make ->atomic_ope...
2405
  	if (error) {	/* returned 1, that is */
30d904947   Al Viro   kill struct opendata
2406
  		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb6   Al Viro   fs/namei.c: get d...
2407
  			error = -EIO;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2408
2409
  			goto out;
  		}
30d904947   Al Viro   kill struct opendata
2410
  		if (file->f_path.dentry) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2411
  			dput(dentry);
30d904947   Al Viro   kill struct opendata
2412
  			dentry = file->f_path.dentry;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2413
  		}
62b2ce964   Sage Weil   vfs: fix propagat...
2414
2415
2416
2417
  		if (create_error && dentry->d_inode == NULL) {
  			error = create_error;
  			goto out;
  		}
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2418
2419
2420
2421
2422
2423
2424
  		goto looked_up;
  	}
  
  	/*
  	 * We didn't have the inode before the open, so check open permission
  	 * here.
  	 */
2675a4eb6   Al Viro   fs/namei.c: get d...
2425
2426
2427
  	error = may_open(&file->f_path, acc_mode, open_flag);
  	if (error)
  		fput(file);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2428
2429
2430
  
  out:
  	dput(dentry);
2675a4eb6   Al Viro   fs/namei.c: get d...
2431
  	return error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2432

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2433
2434
  no_open:
  	if (need_lookup) {
72bd866a0   Al Viro   fs/namei.c: don't...
2435
  		dentry = lookup_real(dir, dentry, nd->flags);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2436
  		if (IS_ERR(dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2437
  			return PTR_ERR(dentry);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2438
2439
2440
  
  		if (create_error) {
  			int open_flag = op->open_flag;
2675a4eb6   Al Viro   fs/namei.c: get d...
2441
  			error = create_error;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
  			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...
2457
  	return 1;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2458
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2459
  /*
1acf0af9b   David Howells   VFS: Fix the bann...
2460
   * Look up and maybe create and open the last component.
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2461
2462
2463
   *
   * Must be called with i_mutex held on parent.
   *
1acf0af9b   David Howells   VFS: Fix the bann...
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
   * 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...
2476
   */
2675a4eb6   Al Viro   fs/namei.c: get d...
2477
2478
2479
  static int lookup_open(struct nameidata *nd, struct path *path,
  			struct file *file,
  			const struct open_flags *op,
64894cf84   Al Viro   simplify lookup_o...
2480
  			bool got_write, int *opened)
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2481
2482
  {
  	struct dentry *dir = nd->path.dentry;
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2483
  	struct inode *dir_inode = dir->d_inode;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2484
2485
  	struct dentry *dentry;
  	int error;
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2486
  	bool need_lookup;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2487

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

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2493
2494
2495
2496
2497
  	/* 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...
2498
  		return atomic_open(nd, dentry, path, file, op, got_write,
47237687d   Al Viro   ->atomic_open() p...
2499
  				   need_lookup, opened);
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2500
  	}
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2501
2502
  	if (need_lookup) {
  		BUG_ON(dentry->d_inode);
72bd866a0   Al Viro   fs/namei.c: don't...
2503
  		dentry = lookup_real(dir_inode, dentry, nd->flags);
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2504
  		if (IS_ERR(dentry))
2675a4eb6   Al Viro   fs/namei.c: get d...
2505
  			return PTR_ERR(dentry);
54ef48724   Miklos Szeredi   vfs: lookup_open(...
2506
  	}
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
  	/* 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 ...
2517
  		 * the 'struct file' in finish_open().
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2518
  		 */
64894cf84   Al Viro   simplify lookup_o...
2519
2520
  		if (!got_write) {
  			error = -EROFS;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2521
  			goto out_dput;
64894cf84   Al Viro   simplify lookup_o...
2522
  		}
47237687d   Al Viro   ->atomic_open() p...
2523
  		*opened |= FILE_CREATED;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2524
2525
2526
  		error = security_path_mknod(&nd->path, dentry, mode, 0);
  		if (error)
  			goto out_dput;
312b63fba   Al Viro   don't pass nameid...
2527
2528
  		error = vfs_create(dir->d_inode, dentry, mode,
  				   nd->flags & LOOKUP_EXCL);
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2529
2530
2531
  		if (error)
  			goto out_dput;
  	}
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2532
  out_no_open:
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2533
2534
  	path->dentry = dentry;
  	path->mnt = nd->path.mnt;
2675a4eb6   Al Viro   fs/namei.c: get d...
2535
  	return 1;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2536
2537
2538
  
  out_dput:
  	dput(dentry);
2675a4eb6   Al Viro   fs/namei.c: get d...
2539
  	return error;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2540
2541
2542
  }
  
  /*
fe2d35ff0   Al Viro   switch non-create...
2543
   * Handle the last step of open()
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2544
   */
2675a4eb6   Al Viro   fs/namei.c: get d...
2545
2546
  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...
2547
  		   int *opened, struct filename *name)
fb1cc555d   Al Viro   gut do_filp_open(...
2548
  {
a1e28038d   Al Viro   pull the common p...
2549
  	struct dentry *dir = nd->path.dentry;
ca344a894   Al Viro   do_last: unify ma...
2550
  	int open_flag = op->open_flag;
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2551
  	bool will_truncate = (open_flag & O_TRUNC) != 0;
64894cf84   Al Viro   simplify lookup_o...
2552
  	bool got_write = false;
bcda76524   Al Viro   Allow O_PATH for ...
2553
  	int acc_mode = op->acc_mode;
a1eb33153   Miklos Szeredi   vfs: do_last(): i...
2554
  	struct inode *inode;
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2555
  	bool symlink_ok = false;
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2556
2557
  	struct path save_parent = { .dentry = NULL, .mnt = NULL };
  	bool retried = false;
16c2cd717   Al Viro   untangle the "nee...
2558
  	int error;
1f36f774b   Al Viro   Switch !O_CREAT c...
2559

c3e380b0b   Al Viro   Collect "operatio...
2560
2561
  	nd->flags &= ~LOOKUP_PARENT;
  	nd->flags |= op->intent;
1f36f774b   Al Viro   Switch !O_CREAT c...
2562
2563
  	switch (nd->last_type) {
  	case LAST_DOTDOT:
176306f59   Neil Brown   VFS: fix recent b...
2564
  	case LAST_DOT:
fe2d35ff0   Al Viro   switch non-create...
2565
2566
  		error = handle_dots(nd, nd->last_type);
  		if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2567
  			return error;
1f36f774b   Al Viro   Switch !O_CREAT c...
2568
  		/* fallthrough */
1f36f774b   Al Viro   Switch !O_CREAT c...
2569
  	case LAST_ROOT:
9f1fafee9   Al Viro   merge handle_reva...
2570
  		error = complete_walk(nd);
16c2cd717   Al Viro   untangle the "nee...
2571
  		if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2572
  			return error;
adb5c2473   Jeff Layton   audit: make audit...
2573
  		audit_inode(name, nd->path.dentry, 0);
ca344a894   Al Viro   do_last: unify ma...
2574
  		if (open_flag & O_CREAT) {
fe2d35ff0   Al Viro   switch non-create...
2575
  			error = -EISDIR;
2675a4eb6   Al Viro   fs/namei.c: get d...
2576
  			goto out;
fe2d35ff0   Al Viro   switch non-create...
2577
  		}
e83db1672   Miklos Szeredi   vfs: do_last(): c...
2578
  		goto finish_open;
1f36f774b   Al Viro   Switch !O_CREAT c...
2579
  	case LAST_BIND:
9f1fafee9   Al Viro   merge handle_reva...
2580
  		error = complete_walk(nd);
16c2cd717   Al Viro   untangle the "nee...
2581
  		if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2582
  			return error;
adb5c2473   Jeff Layton   audit: make audit...
2583
  		audit_inode(name, dir, 0);
e83db1672   Miklos Szeredi   vfs: do_last(): c...
2584
  		goto finish_open;
1f36f774b   Al Viro   Switch !O_CREAT c...
2585
  	}
67ee3ad21   Al Viro   Pull handling of ...
2586

ca344a894   Al Viro   do_last: unify ma...
2587
  	if (!(open_flag & O_CREAT)) {
fe2d35ff0   Al Viro   switch non-create...
2588
2589
  		if (nd->last.name[nd->last.len])
  			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
bcda76524   Al Viro   Allow O_PATH for ...
2590
  		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2591
  			symlink_ok = true;
fe2d35ff0   Al Viro   switch non-create...
2592
  		/* we _can_ be in RCU mode here */
e97cdc87b   Al Viro   lookup_fast: get ...
2593
  		error = lookup_fast(nd, path, &inode);
715748654   Miklos Szeredi   vfs: do_last(): c...
2594
2595
2596
2597
  		if (likely(!error))
  			goto finish_lookup;
  
  		if (error < 0)
2675a4eb6   Al Viro   fs/namei.c: get d...
2598
  			goto out;
715748654   Miklos Szeredi   vfs: do_last(): c...
2599
2600
  
  		BUG_ON(nd->inode != dir->d_inode);
b6183df7b   Miklos Szeredi   vfs: do_last(): s...
2601
2602
2603
2604
2605
2606
2607
2608
2609
  	} 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...
2610
  			return error;
fe2d35ff0   Al Viro   switch non-create...
2611

adb5c2473   Jeff Layton   audit: make audit...
2612
  		audit_inode(name, dir, 0);
b6183df7b   Miklos Szeredi   vfs: do_last(): s...
2613
2614
2615
  		error = -EISDIR;
  		/* trailing slashes? */
  		if (nd->last.name[nd->last.len])
2675a4eb6   Al Viro   fs/namei.c: get d...
2616
  			goto out;
b6183df7b   Miklos Szeredi   vfs: do_last(): s...
2617
  	}
a2c36b450   Al Viro   pull more into do...
2618

16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2619
  retry_lookup:
64894cf84   Al Viro   simplify lookup_o...
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
  	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...
2630
  	mutex_lock(&dir->d_inode->i_mutex);
64894cf84   Al Viro   simplify lookup_o...
2631
  	error = lookup_open(nd, path, file, op, got_write, opened);
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2632
  	mutex_unlock(&dir->d_inode->i_mutex);
a1e28038d   Al Viro   pull the common p...
2633

2675a4eb6   Al Viro   fs/namei.c: get d...
2634
2635
  	if (error <= 0) {
  		if (error)
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2636
  			goto out;
47237687d   Al Viro   ->atomic_open() p...
2637
  		if ((*opened & FILE_CREATED) ||
496ad9aa8   Al Viro   new helper: file_...
2638
  		    !S_ISREG(file_inode(file)->i_mode))
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2639
  			will_truncate = false;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2640

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

47237687d   Al Viro   ->atomic_open() p...
2645
  	if (*opened & FILE_CREATED) {
9b44f1b39   Al Viro   move may_open() f...
2646
  		/* Don't check for write permission, don't truncate */
ca344a894   Al Viro   do_last: unify ma...
2647
  		open_flag &= ~O_TRUNC;
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2648
  		will_truncate = false;
bcda76524   Al Viro   Allow O_PATH for ...
2649
  		acc_mode = MAY_OPEN;
d58ffd35c   Miklos Szeredi   vfs: add lookup_o...
2650
  		path_to_nameidata(path, nd);
e83db1672   Miklos Szeredi   vfs: do_last(): c...
2651
  		goto finish_open_created;
fb1cc555d   Al Viro   gut do_filp_open(...
2652
2653
2654
  	}
  
  	/*
3134f37e9   Jeff Layton   vfs: don't let do...
2655
  	 * create/update audit record if it already exists.
fb1cc555d   Al Viro   gut do_filp_open(...
2656
  	 */
3134f37e9   Jeff Layton   vfs: don't let do...
2657
  	if (path->dentry->d_inode)
adb5c2473   Jeff Layton   audit: make audit...
2658
  		audit_inode(name, path->dentry, 0);
fb1cc555d   Al Viro   gut do_filp_open(...
2659

d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2660
2661
2662
2663
2664
  	/*
  	 * 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...
2665
  	if (got_write) {
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2666
  		mnt_drop_write(nd->path.mnt);
64894cf84   Al Viro   simplify lookup_o...
2667
  		got_write = false;
d18e9008c   Miklos Szeredi   vfs: add i_op->at...
2668
  	}
fb1cc555d   Al Viro   gut do_filp_open(...
2669
  	error = -EEXIST;
f8310c592   Al Viro   fix O_EXCL handli...
2670
  	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
fb1cc555d   Al Viro   gut do_filp_open(...
2671
  		goto exit_dput;
9875cf806   David Howells   Add a dentry op t...
2672
2673
2674
  	error = follow_managed(path, nd->flags);
  	if (error < 0)
  		goto exit_dput;
fb1cc555d   Al Viro   gut do_filp_open(...
2675

a3fbbde70   Al Viro   VFS: we need to s...
2676
2677
  	if (error)
  		nd->flags |= LOOKUP_JUMPED;
decf34008   Miklos Szeredi   vfs: do_last(): u...
2678
2679
  	BUG_ON(nd->flags & LOOKUP_RCU);
  	inode = path->dentry->d_inode;
5f5daac12   Miklos Szeredi   vfs: do_last() co...
2680
2681
  finish_lookup:
  	/* we _can_ be in RCU mode here */
fb1cc555d   Al Viro   gut do_filp_open(...
2682
  	error = -ENOENT;
54c33e7f9   Miklos Szeredi   vfs: do_last(): m...
2683
2684
  	if (!inode) {
  		path_to_nameidata(path, nd);
2675a4eb6   Al Viro   fs/namei.c: get d...
2685
  		goto out;
54c33e7f9   Miklos Szeredi   vfs: do_last(): m...
2686
  	}
9e67f3616   Al Viro   Kill is_link argu...
2687

d45ea8679   Miklos Szeredi   vfs: make follow_...
2688
2689
2690
2691
  	if (should_follow_link(inode, !symlink_ok)) {
  		if (nd->flags & LOOKUP_RCU) {
  			if (unlikely(unlazy_walk(nd, path->dentry))) {
  				error = -ECHILD;
2675a4eb6   Al Viro   fs/namei.c: get d...
2692
  				goto out;
d45ea8679   Miklos Szeredi   vfs: make follow_...
2693
2694
2695
  			}
  		}
  		BUG_ON(inode != path->dentry->d_inode);
2675a4eb6   Al Viro   fs/namei.c: get d...
2696
  		return 1;
d45ea8679   Miklos Szeredi   vfs: make follow_...
2697
  	}
fb1cc555d   Al Viro   gut do_filp_open(...
2698

16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2699
2700
2701
2702
2703
2704
2705
2706
  	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...
2707
  	nd->inode = inode;
a3fbbde70   Al Viro   VFS: we need to s...
2708
2709
  	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
  	error = complete_walk(nd);
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2710
2711
  	if (error) {
  		path_put(&save_parent);
2675a4eb6   Al Viro   fs/namei.c: get d...
2712
  		return error;
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2713
  	}
fb1cc555d   Al Viro   gut do_filp_open(...
2714
  	error = -EISDIR;
050ac841e   Miklos Szeredi   vfs: do_last(): o...
2715
  	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
2675a4eb6   Al Viro   fs/namei.c: get d...
2716
  		goto out;
af2f55426   Miklos Szeredi   vfs: do_last(): c...
2717
2718
  	error = -ENOTDIR;
  	if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
2675a4eb6   Al Viro   fs/namei.c: get d...
2719
  		goto out;
adb5c2473   Jeff Layton   audit: make audit...
2720
  	audit_inode(name, nd->path.dentry, 0);
e83db1672   Miklos Szeredi   vfs: do_last(): c...
2721
  finish_open:
6c0d46c49   Al Viro   fold __open_namei...
2722
  	if (!S_ISREG(nd->inode->i_mode))
77d660a8a   Miklos Szeredi   vfs: do_last(): c...
2723
  		will_truncate = false;
6c0d46c49   Al Viro   fold __open_namei...
2724

0f9d1a10c   Al Viro   expand finish_ope...
2725
2726
2727
  	if (will_truncate) {
  		error = mnt_want_write(nd->path.mnt);
  		if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2728
  			goto out;
64894cf84   Al Viro   simplify lookup_o...
2729
  		got_write = true;
0f9d1a10c   Al Viro   expand finish_ope...
2730
  	}
e83db1672   Miklos Szeredi   vfs: do_last(): c...
2731
  finish_open_created:
bcda76524   Al Viro   Allow O_PATH for ...
2732
  	error = may_open(&nd->path, acc_mode, open_flag);
ca344a894   Al Viro   do_last: unify ma...
2733
  	if (error)
2675a4eb6   Al Viro   fs/namei.c: get d...
2734
  		goto out;
30d904947   Al Viro   kill struct opendata
2735
2736
2737
  	file->f_path.mnt = nd->path.mnt;
  	error = finish_open(file, nd->path.dentry, NULL, opened);
  	if (error) {
30d904947   Al Viro   kill struct opendata
2738
  		if (error == -EOPENSTALE)
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2739
  			goto stale_open;
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2740
  		goto out;
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2741
  	}
a8277b9ba   Miklos Szeredi   vfs: move O_DIREC...
2742
  opened:
2675a4eb6   Al Viro   fs/namei.c: get d...
2743
  	error = open_check_o_direct(file);
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2744
2745
  	if (error)
  		goto exit_fput;
2675a4eb6   Al Viro   fs/namei.c: get d...
2746
  	error = ima_file_check(file, op->acc_mode);
aa4caadb7   Miklos Szeredi   vfs: do_last(): c...
2747
2748
2749
2750
  	if (error)
  		goto exit_fput;
  
  	if (will_truncate) {
2675a4eb6   Al Viro   fs/namei.c: get d...
2751
  		error = handle_truncate(file);
aa4caadb7   Miklos Szeredi   vfs: do_last(): c...
2752
2753
  		if (error)
  			goto exit_fput;
0f9d1a10c   Al Viro   expand finish_ope...
2754
  	}
ca344a894   Al Viro   do_last: unify ma...
2755
  out:
64894cf84   Al Viro   simplify lookup_o...
2756
  	if (got_write)
0f9d1a10c   Al Viro   expand finish_ope...
2757
  		mnt_drop_write(nd->path.mnt);
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2758
  	path_put(&save_parent);
e276ae672   Miklos Szeredi   vfs: do_last(): m...
2759
  	terminate_walk(nd);
2675a4eb6   Al Viro   fs/namei.c: get d...
2760
  	return error;
fb1cc555d   Al Viro   gut do_filp_open(...
2761

fb1cc555d   Al Viro   gut do_filp_open(...
2762
2763
  exit_dput:
  	path_put_conditional(path, nd);
ca344a894   Al Viro   do_last: unify ma...
2764
  	goto out;
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2765
  exit_fput:
2675a4eb6   Al Viro   fs/namei.c: get d...
2766
2767
  	fput(file);
  	goto out;
015c3bbcd   Miklos Szeredi   vfs: remove open ...
2768

f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
  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...
2780
  	if (got_write) {
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2781
  		mnt_drop_write(nd->path.mnt);
64894cf84   Al Viro   simplify lookup_o...
2782
  		got_write = false;
f60dc3db6   Miklos Szeredi   vfs: do_last(): c...
2783
2784
2785
  	}
  	retried = true;
  	goto retry_lookup;
fb1cc555d   Al Viro   gut do_filp_open(...
2786
  }
669abf4e5   Jeff Layton   vfs: make path_op...
2787
  static struct file *path_openat(int dfd, struct filename *pathname,
73d049a40   Al Viro   open-style analog...
2788
  		struct nameidata *nd, const struct open_flags *op, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2789
  {
fe2d35ff0   Al Viro   switch non-create...
2790
  	struct file *base = NULL;
30d904947   Al Viro   kill struct opendata
2791
  	struct file *file;
9850c0565   Al Viro   Fix the -ESTALE h...
2792
  	struct path path;
47237687d   Al Viro   ->atomic_open() p...
2793
  	int opened = 0;
13aab428a   Al Viro   separate -ESTALE/...
2794
  	int error;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2795

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

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

669abf4e5   Jeff Layton   vfs: make path_op...
2802
  	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2803
  	if (unlikely(error))
2675a4eb6   Al Viro   fs/namei.c: get d...
2804
  		goto out;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2805

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

2675a4eb6   Al Viro   fs/namei.c: get d...
2811
2812
  	error = do_last(nd, &path, file, op, &opened, pathname);
  	while (unlikely(error > 0)) { /* trailing symlink */
7b9337aaf   Nick Piggin   fs: namei fix ->p...
2813
  		struct path link = path;
def4af30c   Al Viro   Get rid of symlin...
2814
  		void *cookie;
574197e0d   Al Viro   tidy the trailing...
2815
  		if (!(nd->flags & LOOKUP_FOLLOW)) {
73d049a40   Al Viro   open-style analog...
2816
2817
  			path_put_conditional(&path, nd);
  			path_put(&nd->path);
2675a4eb6   Al Viro   fs/namei.c: get d...
2818
  			error = -ELOOP;
40b39136f   Al Viro   path_openat: clea...
2819
2820
  			break;
  		}
800179c9b   Kees Cook   fs: add link rest...
2821
2822
2823
  		error = may_follow_link(&link, nd);
  		if (unlikely(error))
  			break;
73d049a40   Al Viro   open-style analog...
2824
2825
  		nd->flags |= LOOKUP_PARENT;
  		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
574197e0d   Al Viro   tidy the trailing...
2826
  		error = follow_link(&link, nd, &cookie);
c3e380b0b   Al Viro   Collect "operatio...
2827
  		if (unlikely(error))
2675a4eb6   Al Viro   fs/namei.c: get d...
2828
2829
  			break;
  		error = do_last(nd, &path, file, op, &opened, pathname);
574197e0d   Al Viro   tidy the trailing...
2830
  		put_link(nd, &link, cookie);
806b681cb   Al Viro   Turn do_link spag...
2831
  	}
10fa8e62f   Al Viro   Unify exits in O_...
2832
  out:
73d049a40   Al Viro   open-style analog...
2833
2834
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  		path_put(&nd->root);
fe2d35ff0   Al Viro   switch non-create...
2835
2836
  	if (base)
  		fput(base);
2675a4eb6   Al Viro   fs/namei.c: get d...
2837
2838
  	if (!(opened & FILE_OPENED)) {
  		BUG_ON(!error);
30d904947   Al Viro   kill struct opendata
2839
  		put_filp(file);
16b1c1cd7   Miklos Szeredi   vfs: retry last c...
2840
  	}
2675a4eb6   Al Viro   fs/namei.c: get d...
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
  	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
2851
  }
669abf4e5   Jeff Layton   vfs: make path_op...
2852
  struct file *do_filp_open(int dfd, struct filename *pathname,
13aab428a   Al Viro   separate -ESTALE/...
2853
2854
  		const struct open_flags *op, int flags)
  {
73d049a40   Al Viro   open-style analog...
2855
  	struct nameidata nd;
13aab428a   Al Viro   separate -ESTALE/...
2856
  	struct file *filp;
73d049a40   Al Viro   open-style analog...
2857
  	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
13aab428a   Al Viro   separate -ESTALE/...
2858
  	if (unlikely(filp == ERR_PTR(-ECHILD)))
73d049a40   Al Viro   open-style analog...
2859
  		filp = path_openat(dfd, pathname, &nd, op, flags);
13aab428a   Al Viro   separate -ESTALE/...
2860
  	if (unlikely(filp == ERR_PTR(-ESTALE)))
73d049a40   Al Viro   open-style analog...
2861
  		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
13aab428a   Al Viro   separate -ESTALE/...
2862
2863
  	return filp;
  }
73d049a40   Al Viro   open-style analog...
2864
2865
2866
2867
2868
  struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  		const char *name, const struct open_flags *op, int flags)
  {
  	struct nameidata nd;
  	struct file *file;
669abf4e5   Jeff Layton   vfs: make path_op...
2869
  	struct filename filename = { .name = name };
73d049a40   Al Viro   open-style analog...
2870
2871
2872
2873
2874
  
  	nd.root.mnt = mnt;
  	nd.root.dentry = dentry;
  
  	flags |= LOOKUP_ROOT;
bcda76524   Al Viro   Allow O_PATH for ...
2875
  	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
73d049a40   Al Viro   open-style analog...
2876
  		return ERR_PTR(-ELOOP);
669abf4e5   Jeff Layton   vfs: make path_op...
2877
  	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
73d049a40   Al Viro   open-style analog...
2878
  	if (unlikely(file == ERR_PTR(-ECHILD)))
669abf4e5   Jeff Layton   vfs: make path_op...
2879
  		file = path_openat(-1, &filename, &nd, op, flags);
73d049a40   Al Viro   open-style analog...
2880
  	if (unlikely(file == ERR_PTR(-ESTALE)))
669abf4e5   Jeff Layton   vfs: make path_op...
2881
  		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
73d049a40   Al Viro   open-style analog...
2882
2883
  	return file;
  }
1ac12b4b6   Jeff Layton   vfs: turn is_dir ...
2884
2885
  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
2886
  {
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2887
  	struct dentry *dentry = ERR_PTR(-EEXIST);
ed75e95de   Al Viro   kill lookup_create()
2888
  	struct nameidata nd;
c30dabfe5   Jan Kara   fs: Push mnt_want...
2889
  	int err2;
1ac12b4b6   Jeff Layton   vfs: turn is_dir ...
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
  	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()
2900
2901
  	if (error)
  		return ERR_PTR(error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2902

c663e5d80   Christoph Hellwig   [PATCH] add some ...
2903
2904
2905
2906
  	/*
  	 * Yucky last component or no last component at all?
  	 * (foo/., foo/.., /////)
  	 */
ed75e95de   Al Viro   kill lookup_create()
2907
2908
2909
2910
  	if (nd.last_type != LAST_NORM)
  		goto out;
  	nd.flags &= ~LOOKUP_PARENT;
  	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2911

c30dabfe5   Jan Kara   fs: Push mnt_want...
2912
2913
  	/* 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 ...
2914
2915
2916
  	/*
  	 * Do the final lookup.
  	 */
ed75e95de   Al Viro   kill lookup_create()
2917
2918
  	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
2919
  	if (IS_ERR(dentry))
a8104a9fc   Al Viro   pull mnt_want_wri...
2920
  		goto unlock;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2921

a8104a9fc   Al Viro   pull mnt_want_wri...
2922
  	error = -EEXIST;
e9baf6e59   Al Viro   [PATCH] return to...
2923
  	if (dentry->d_inode)
a8104a9fc   Al Viro   pull mnt_want_wri...
2924
  		goto fail;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2925
2926
2927
2928
2929
2930
  	/*
  	 * 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()
2931
  	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
a8104a9fc   Al Viro   pull mnt_want_wri...
2932
  		error = -ENOENT;
ed75e95de   Al Viro   kill lookup_create()
2933
  		goto fail;
e9baf6e59   Al Viro   [PATCH] return to...
2934
  	}
c30dabfe5   Jan Kara   fs: Push mnt_want...
2935
2936
  	if (unlikely(err2)) {
  		error = err2;
a8104a9fc   Al Viro   pull mnt_want_wri...
2937
  		goto fail;
c30dabfe5   Jan Kara   fs: Push mnt_want...
2938
  	}
ed75e95de   Al Viro   kill lookup_create()
2939
  	*path = nd.path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2940
  	return dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2941
  fail:
a8104a9fc   Al Viro   pull mnt_want_wri...
2942
2943
2944
  	dput(dentry);
  	dentry = ERR_PTR(error);
  unlock:
ed75e95de   Al Viro   kill lookup_create()
2945
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
c30dabfe5   Jan Kara   fs: Push mnt_want...
2946
2947
  	if (!err2)
  		mnt_drop_write(nd.path.mnt);
ed75e95de   Al Viro   kill lookup_create()
2948
2949
  out:
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2950
2951
  	return dentry;
  }
dae6ad8f3   Al Viro   new helpers: kern...
2952
  EXPORT_SYMBOL(kern_path_create);
921a1650d   Al Viro   new helper: done_...
2953
2954
2955
2956
  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...
2957
  	mnt_drop_write(path->mnt);
921a1650d   Al Viro   new helper: done_...
2958
2959
2960
  	path_put(path);
  }
  EXPORT_SYMBOL(done_path_create);
1ac12b4b6   Jeff Layton   vfs: turn is_dir ...
2961
2962
  struct dentry *user_path_create(int dfd, const char __user *pathname,
  				struct path *path, unsigned int lookup_flags)
dae6ad8f3   Al Viro   new helpers: kern...
2963
  {
91a27b2a7   Jeff Layton   vfs: define struc...
2964
  	struct filename *tmp = getname(pathname);
dae6ad8f3   Al Viro   new helpers: kern...
2965
2966
2967
  	struct dentry *res;
  	if (IS_ERR(tmp))
  		return ERR_CAST(tmp);
1ac12b4b6   Jeff Layton   vfs: turn is_dir ...
2968
  	res = kern_path_create(dfd, tmp->name, path, lookup_flags);
dae6ad8f3   Al Viro   new helpers: kern...
2969
2970
2971
2972
  	putname(tmp);
  	return res;
  }
  EXPORT_SYMBOL(user_path_create);
1a67aafb5   Al Viro   switch ->mknod() ...
2973
  int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2974
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2975
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2976
2977
2978
  
  	if (error)
  		return error;
975d6b393   Eric W. Biederman   vfs: Don't allow ...
2979
  	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2980
  		return -EPERM;
acfa4380e   Al Viro   inode->i_op is ne...
2981
  	if (!dir->i_op->mknod)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2982
  		return -EPERM;
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
2983
2984
2985
  	error = devcgroup_inode_mknod(mode, dev);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2986
2987
2988
  	error = security_inode_mknod(dir, dentry, mode, dev);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2989
  	error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
2990
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
2991
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2992
2993
  	return error;
  }
f69aac000   Al Viro   switch may_mknod(...
2994
  static int may_mknod(umode_t mode)
463c31972   Dave Hansen   [PATCH] r/o bind ...
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
  {
  	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...
3010
  SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3011
  		unsigned, dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3012
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3013
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3014
3015
  	struct path path;
  	int error;
972567f14   Jeff Layton   vfs: fix mknodat ...
3016
  	unsigned int lookup_flags = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3017

8e4bfca1d   Al Viro   mknod: take sanit...
3018
3019
3020
  	error = may_mknod(mode);
  	if (error)
  		return error;
972567f14   Jeff Layton   vfs: fix mknodat ...
3021
3022
  retry:
  	dentry = user_path_create(dfd, filename, &path, lookup_flags);
dae6ad8f3   Al Viro   new helpers: kern...
3023
3024
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
2ad94ae65   Al Viro   [PATCH] new (loca...
3025

dae6ad8f3   Al Viro   new helpers: kern...
3026
  	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5   Al Viro   New helper - curr...
3027
  		mode &= ~current_umask();
dae6ad8f3   Al Viro   new helpers: kern...
3028
  	error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56a   Kentaro Takeda   introduce new LSM...
3029
  	if (error)
a8104a9fc   Al Viro   pull mnt_want_wri...
3030
  		goto out;
463c31972   Dave Hansen   [PATCH] r/o bind ...
3031
  	switch (mode & S_IFMT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3032
  		case 0: case S_IFREG:
312b63fba   Al Viro   don't pass nameid...
3033
  			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3034
3035
  			break;
  		case S_IFCHR: case S_IFBLK:
dae6ad8f3   Al Viro   new helpers: kern...
3036
  			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3037
3038
3039
  					new_decode_dev(dev));
  			break;
  		case S_IFIFO: case S_IFSOCK:
dae6ad8f3   Al Viro   new helpers: kern...
3040
  			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3041
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3042
  	}
a8104a9fc   Al Viro   pull mnt_want_wri...
3043
  out:
921a1650d   Al Viro   new helper: done_...
3044
  	done_path_create(&path, dentry);
972567f14   Jeff Layton   vfs: fix mknodat ...
3045
3046
3047
3048
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3049
3050
  	return error;
  }
8208a22bb   Al Viro   switch sys_mknoda...
3051
  SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3052
3053
3054
  {
  	return sys_mknodat(AT_FDCWD, filename, mode, dev);
  }
18bb1db3e   Al Viro   switch vfs_mkdir(...
3055
  int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3056
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3057
  	int error = may_create(dir, dentry);
8de527787   Al Viro   vfs: check i_nlin...
3058
  	unsigned max_links = dir->i_sb->s_max_links;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3059
3060
3061
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3062
  	if (!dir->i_op->mkdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3063
3064
3065
3066
3067
3068
  		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...
3069
3070
  	if (max_links && dir->i_nlink >= max_links)
  		return -EMLINK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3071
  	error = dir->i_op->mkdir(dir, dentry, mode);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
3072
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
3073
  		fsnotify_mkdir(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3074
3075
  	return error;
  }
a218d0fdc   Al Viro   switch open and m...
3076
  SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3077
  {
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3078
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3079
3080
  	struct path path;
  	int error;
b76d8b822   Jeff Layton   vfs: fix mkdirat ...
3081
  	unsigned int lookup_flags = LOOKUP_DIRECTORY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3082

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

dae6ad8f3   Al Viro   new helpers: kern...
3088
  	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5   Al Viro   New helper - curr...
3089
  		mode &= ~current_umask();
dae6ad8f3   Al Viro   new helpers: kern...
3090
  	error = security_path_mkdir(&path, dentry, mode);
a8104a9fc   Al Viro   pull mnt_want_wri...
3091
3092
  	if (!error)
  		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
921a1650d   Al Viro   new helper: done_...
3093
  	done_path_create(&path, dentry);
b76d8b822   Jeff Layton   vfs: fix mkdirat ...
3094
3095
3096
3097
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3098
3099
  	return error;
  }
a218d0fdc   Al Viro   switch open and m...
3100
  SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3101
3102
3103
  {
  	return sys_mkdirat(AT_FDCWD, pathname, mode);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3104
  /*
a71905f0d   Sage Weil   vfs: update dentr...
3105
   * The dentry_unhash() helper will try to drop the dentry early: we
c0d025948   J. Bruce Fields   vfs: fix out-of-d...
3106
   * should have a usage count of 1 if we're the only user of this
a71905f0d   Sage Weil   vfs: update dentr...
3107
3108
   * 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
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
   *
   * 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...
3121
  	shrink_dcache_parent(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3122
  	spin_lock(&dentry->d_lock);
64252c75a   Sage Weil   vfs: remove dget(...
3123
  	if (dentry->d_count == 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3124
3125
  		__d_drop(dentry);
  	spin_unlock(&dentry->d_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3126
3127
3128
3129
3130
3131
3132
3133
  }
  
  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...
3134
  	if (!dir->i_op->rmdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3135
  		return -EPERM;
1d2ef5901   Al Viro   restore pinning t...
3136
  	dget(dentry);
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3137
  	mutex_lock(&dentry->d_inode->i_mutex);
912dbc15d   Sage Weil   vfs: clean up vfs...
3138
3139
  
  	error = -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3140
  	if (d_mountpoint(dentry))
912dbc15d   Sage Weil   vfs: clean up vfs...
3141
3142
3143
3144
3145
  		goto out;
  
  	error = security_inode_rmdir(dir, dentry);
  	if (error)
  		goto out;
3cebde241   Sage Weil   vfs: shrink_dcach...
3146
  	shrink_dcache_parent(dentry);
912dbc15d   Sage Weil   vfs: clean up vfs...
3147
3148
3149
3150
3151
3152
3153
3154
  	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...
3155
  	mutex_unlock(&dentry->d_inode->i_mutex);
1d2ef5901   Al Viro   restore pinning t...
3156
  	dput(dentry);
912dbc15d   Sage Weil   vfs: clean up vfs...
3157
  	if (!error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3158
  		d_delete(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3159
3160
  	return error;
  }
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3161
  static long do_rmdir(int dfd, const char __user *pathname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3162
3163
  {
  	int error = 0;
91a27b2a7   Jeff Layton   vfs: define struc...
3164
  	struct filename *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3165
3166
  	struct dentry *dentry;
  	struct nameidata nd;
c6ee92069   Jeff Layton   vfs: make do_rmdi...
3167
3168
3169
  	unsigned int lookup_flags = 0;
  retry:
  	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
3170
3171
  	if (IS_ERR(name))
  		return PTR_ERR(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3172
3173
  
  	switch(nd.last_type) {
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3174
3175
3176
3177
3178
3179
3180
3181
3182
  	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
3183
  	}
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3184
3185
  
  	nd.flags &= ~LOOKUP_PARENT;
c30dabfe5   Jan Kara   fs: Push mnt_want...
3186
3187
3188
  	error = mnt_want_write(nd.path.mnt);
  	if (error)
  		goto exit1;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3189

4ac913785   Jan Blunck   Embed a struct pa...
3190
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
3191
  	dentry = lookup_hash(&nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3192
  	error = PTR_ERR(dentry);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3193
3194
  	if (IS_ERR(dentry))
  		goto exit2;
e6bc45d65   Theodore Ts'o   vfs: make unlink(...
3195
3196
3197
3198
  	if (!dentry->d_inode) {
  		error = -ENOENT;
  		goto exit3;
  	}
be6d3e56a   Kentaro Takeda   introduce new LSM...
3199
3200
  	error = security_path_rmdir(&nd.path, dentry);
  	if (error)
c30dabfe5   Jan Kara   fs: Push mnt_want...
3201
  		goto exit3;
4ac913785   Jan Blunck   Embed a struct pa...
3202
  	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
0622753b8   Dave Hansen   [PATCH] r/o bind ...
3203
  exit3:
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3204
3205
  	dput(dentry);
  exit2:
4ac913785   Jan Blunck   Embed a struct pa...
3206
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
c30dabfe5   Jan Kara   fs: Push mnt_want...
3207
  	mnt_drop_write(nd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3208
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
3209
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3210
  	putname(name);
c6ee92069   Jeff Layton   vfs: make do_rmdi...
3211
3212
3213
3214
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3215
3216
  	return error;
  }
3cdad4288   Heiko Carstens   [CVE-2009-0029] S...
3217
  SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3218
3219
3220
  {
  	return do_rmdir(AT_FDCWD, pathname);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3221
3222
3223
3224
3225
3226
  int vfs_unlink(struct inode *dir, struct dentry *dentry)
  {
  	int error = may_delete(dir, dentry, 0);
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3227
  	if (!dir->i_op->unlink)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3228
  		return -EPERM;
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3229
  	mutex_lock(&dentry->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3230
3231
3232
3233
  	if (d_mountpoint(dentry))
  		error = -EBUSY;
  	else {
  		error = security_inode_unlink(dir, dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3234
  		if (!error) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3235
  			error = dir->i_op->unlink(dir, dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3236
  			if (!error)
d83c49f3e   Al Viro   Fix the regressio...
3237
  				dont_mount(dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
3238
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3239
  	}
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3240
  	mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3241
3242
3243
  
  	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
  	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
ece95912d   Jan Kara   inotify: send IN_...
3244
  		fsnotify_link_count(dentry->d_inode);
e234f35c5   John McCutchan   [PATCH] inotify d...
3245
  		d_delete(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3246
  	}
0eeca2830   Robert Love   [PATCH] inotify
3247

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3248
3249
3250
3251
3252
  	return error;
  }
  
  /*
   * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3253
   * directory's i_mutex.  Truncate can take a long time if there is a lot of
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3254
3255
3256
   * 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 ...
3257
  static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3258
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3259
  	int error;
91a27b2a7   Jeff Layton   vfs: define struc...
3260
  	struct filename *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3261
3262
3263
  	struct dentry *dentry;
  	struct nameidata nd;
  	struct inode *inode = NULL;
5d18f8133   Jeff Layton   vfs: make do_unli...
3264
3265
3266
  	unsigned int lookup_flags = 0;
  retry:
  	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
3267
3268
  	if (IS_ERR(name))
  		return PTR_ERR(name);
2ad94ae65   Al Viro   [PATCH] new (loca...
3269

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3270
3271
3272
  	error = -EISDIR;
  	if (nd.last_type != LAST_NORM)
  		goto exit1;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3273
3274
  
  	nd.flags &= ~LOOKUP_PARENT;
c30dabfe5   Jan Kara   fs: Push mnt_want...
3275
3276
3277
  	error = mnt_want_write(nd.path.mnt);
  	if (error)
  		goto exit1;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3278

4ac913785   Jan Blunck   Embed a struct pa...
3279
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
3280
  	dentry = lookup_hash(&nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3281
3282
3283
  	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...
3284
3285
  		if (nd.last.name[nd.last.len])
  			goto slashes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3286
  		inode = dentry->d_inode;
50338b889   Török Edwin   fix wrong iput on...
3287
  		if (!inode)
e6bc45d65   Theodore Ts'o   vfs: make unlink(...
3288
3289
  			goto slashes;
  		ihold(inode);
be6d3e56a   Kentaro Takeda   introduce new LSM...
3290
3291
  		error = security_path_unlink(&nd.path, dentry);
  		if (error)
c30dabfe5   Jan Kara   fs: Push mnt_want...
3292
  			goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
3293
  		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
c30dabfe5   Jan Kara   fs: Push mnt_want...
3294
  exit2:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3295
3296
  		dput(dentry);
  	}
4ac913785   Jan Blunck   Embed a struct pa...
3297
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3298
3299
  	if (inode)
  		iput(inode);	/* truncate the inode here */
c30dabfe5   Jan Kara   fs: Push mnt_want...
3300
  	mnt_drop_write(nd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3301
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
3302
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3303
  	putname(name);
5d18f8133   Jeff Layton   vfs: make do_unli...
3304
3305
3306
3307
3308
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		inode = NULL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3309
3310
3311
3312
3313
3314
3315
  	return error;
  
  slashes:
  	error = !dentry->d_inode ? -ENOENT :
  		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  	goto exit2;
  }
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3316
  SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3317
3318
3319
3320
3321
3322
3323
3324
3325
  {
  	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...
3326
  SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3327
3328
3329
  {
  	return do_unlinkat(AT_FDCWD, pathname);
  }
db2e747b1   Miklos Szeredi   [patch 5/5] vfs: ...
3330
  int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3331
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3332
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3333
3334
3335
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3336
  	if (!dir->i_op->symlink)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3337
3338
3339
3340
3341
  		return -EPERM;
  
  	error = security_inode_symlink(dir, dentry, oldname);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3342
  	error = dir->i_op->symlink(dir, dentry, oldname);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
3343
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
3344
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3345
3346
  	return error;
  }
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3347
3348
  SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  		int, newdfd, const char __user *, newname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3349
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3350
  	int error;
91a27b2a7   Jeff Layton   vfs: define struc...
3351
  	struct filename *from;
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3352
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3353
  	struct path path;
f46d3567b   Jeff Layton   vfs: fix symlinka...
3354
  	unsigned int lookup_flags = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3355
3356
  
  	from = getname(oldname);
2ad94ae65   Al Viro   [PATCH] new (loca...
3357
  	if (IS_ERR(from))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3358
  		return PTR_ERR(from);
f46d3567b   Jeff Layton   vfs: fix symlinka...
3359
3360
  retry:
  	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3361
3362
  	error = PTR_ERR(dentry);
  	if (IS_ERR(dentry))
dae6ad8f3   Al Viro   new helpers: kern...
3363
  		goto out_putname;
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3364

91a27b2a7   Jeff Layton   vfs: define struc...
3365
  	error = security_path_symlink(&path, dentry, from->name);
a8104a9fc   Al Viro   pull mnt_want_wri...
3366
  	if (!error)
91a27b2a7   Jeff Layton   vfs: define struc...
3367
  		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
921a1650d   Al Viro   new helper: done_...
3368
  	done_path_create(&path, dentry);
f46d3567b   Jeff Layton   vfs: fix symlinka...
3369
3370
3371
3372
  	if (retry_estale(error, lookup_flags)) {
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3373
  out_putname:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3374
3375
3376
  	putname(from);
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
3377
  SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3378
3379
3380
  {
  	return sys_symlinkat(oldname, AT_FDCWD, newname);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3381
3382
3383
  int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  {
  	struct inode *inode = old_dentry->d_inode;
8de527787   Al Viro   vfs: check i_nlin...
3384
  	unsigned max_links = dir->i_sb->s_max_links;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3385
3386
3387
3388
  	int error;
  
  	if (!inode)
  		return -ENOENT;
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3389
  	error = may_create(dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
  	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...
3401
  	if (!dir->i_op->link)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3402
  		return -EPERM;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3403
  	if (S_ISDIR(inode->i_mode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3404
3405
3406
3407
3408
  		return -EPERM;
  
  	error = security_inode_link(old_dentry, dir, new_dentry);
  	if (error)
  		return error;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3409
  	mutex_lock(&inode->i_mutex);
aae8a97d3   Aneesh Kumar K.V   fs: Don't allow t...
3410
3411
3412
  	/* Make sure we don't allow creating hardlink to an unlinked file */
  	if (inode->i_nlink == 0)
  		error =  -ENOENT;
8de527787   Al Viro   vfs: check i_nlin...
3413
3414
  	else if (max_links && inode->i_nlink >= max_links)
  		error = -EMLINK;
aae8a97d3   Aneesh Kumar K.V   fs: Don't allow t...
3415
3416
  	else
  		error = dir->i_op->link(old_dentry, dir, new_dentry);
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3417
  	mutex_unlock(&inode->i_mutex);
e31e14ec3   Stephen Smalley   [PATCH] remove th...
3418
  	if (!error)
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
3419
  		fsnotify_link(dir, inode, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
  	return error;
  }
  
  /*
   * 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...
3432
3433
  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
3434
3435
  {
  	struct dentry *new_dentry;
dae6ad8f3   Al Viro   new helpers: kern...
3436
  	struct path old_path, new_path;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3437
  	int how = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3438
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3439

11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3440
  	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e16   Ulrich Drepper   [PATCH] flags par...
3441
  		return -EINVAL;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
  	/*
  	 * 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.
  	 */
  	if (flags & AT_EMPTY_PATH) {
  		if (!capable(CAP_DAC_READ_SEARCH))
  			return -ENOENT;
  		how = LOOKUP_EMPTY;
  	}
  
  	if (flags & AT_SYMLINK_FOLLOW)
  		how |= LOOKUP_FOLLOW;
442e31ca5   Jeff Layton   vfs: fix linkat t...
3455
  retry:
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
3456
  	error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3457
  	if (error)
2ad94ae65   Al Viro   [PATCH] new (loca...
3458
  		return error;
442e31ca5   Jeff Layton   vfs: fix linkat t...
3459
3460
  	new_dentry = user_path_create(newdfd, newname, &new_path,
  					(how & LOOKUP_REVAL));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3461
  	error = PTR_ERR(new_dentry);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
3462
  	if (IS_ERR(new_dentry))
dae6ad8f3   Al Viro   new helpers: kern...
3463
3464
3465
3466
3467
  		goto out;
  
  	error = -EXDEV;
  	if (old_path.mnt != new_path.mnt)
  		goto out_dput;
800179c9b   Kees Cook   fs: add link rest...
3468
3469
3470
  	error = may_linkat(&old_path);
  	if (unlikely(error))
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
3471
  	error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56a   Kentaro Takeda   introduce new LSM...
3472
  	if (error)
a8104a9fc   Al Viro   pull mnt_want_wri...
3473
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
3474
  	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
3475
  out_dput:
921a1650d   Al Viro   new helper: done_...
3476
  	done_path_create(&new_path, new_dentry);
442e31ca5   Jeff Layton   vfs: fix linkat t...
3477
3478
3479
3480
  	if (retry_estale(error, how)) {
  		how |= LOOKUP_REVAL;
  		goto retry;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3481
  out:
2d8f30380   Al Viro   [PATCH] sanitize ...
3482
  	path_put(&old_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3483
3484
3485
  
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
3486
  SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3487
  {
c04030e16   Ulrich Drepper   [PATCH] flags par...
3488
  	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3489
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3490
3491
3492
3493
3494
3495
3496
  /*
   * The worst of all namespace operations - renaming directory. "Perverted"
   * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
   * Problems:
   *	a) we can get into loop creation. Check is done in is_subdir().
   *	b) race potential - two innocent renames can create a loop together.
   *	   That's where 4.4 screws up. Current fix: serialization on
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
3497
   *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3498
3499
   *	   story.
   *	c) we have to lock _three_ objects - parents and victim (if it exists).
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3500
   *	   And that - after we got ->i_mutex on parents (until then we don't know
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3501
3502
   *	   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...
3503
   *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3504
3505
3506
   *	   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...
3507
   *	   lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3508
3509
3510
   *	   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_...
3511
   *	d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3512
   *	   we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3513
   *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e72   Adam Buchbinder   Fix misspellings ...
3514
   *	   ->i_mutex on parents, which works but leads to some truly excessive
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3515
3516
   *	   locking].
   */
75c96f858   Adrian Bunk   [PATCH] make some...
3517
3518
  static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  			  struct inode *new_dir, struct dentry *new_dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3519
3520
  {
  	int error = 0;
9055cba71   Sage Weil   vfs: clean up vfs...
3521
  	struct inode *target = new_dentry->d_inode;
8de527787   Al Viro   vfs: check i_nlin...
3522
  	unsigned max_links = new_dir->i_sb->s_max_links;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3523
3524
3525
3526
3527
3528
  
  	/*
  	 * If we are going to change the parent - check write permissions,
  	 * we'll need to flip '..'.
  	 */
  	if (new_dir != old_dir) {
f419a2e3b   Al Viro   [PATCH] kill name...
3529
  		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3530
3531
3532
3533
3534
3535
3536
  		if (error)
  			return error;
  	}
  
  	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  	if (error)
  		return error;
1d2ef5901   Al Viro   restore pinning t...
3537
  	dget(new_dentry);
d83c49f3e   Al Viro   Fix the regressio...
3538
  	if (target)
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3539
  		mutex_lock(&target->i_mutex);
9055cba71   Sage Weil   vfs: clean up vfs...
3540
3541
3542
3543
  
  	error = -EBUSY;
  	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  		goto out;
8de527787   Al Viro   vfs: check i_nlin...
3544
3545
3546
3547
  	error = -EMLINK;
  	if (max_links && !target && new_dir != old_dir &&
  	    new_dir->i_nlink >= max_links)
  		goto out;
3cebde241   Sage Weil   vfs: shrink_dcach...
3548
3549
  	if (target)
  		shrink_dcache_parent(new_dentry);
9055cba71   Sage Weil   vfs: clean up vfs...
3550
3551
3552
  	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  	if (error)
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3553
  	if (target) {
9055cba71   Sage Weil   vfs: clean up vfs...
3554
3555
  		target->i_flags |= S_DEAD;
  		dont_mount(new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3556
  	}
9055cba71   Sage Weil   vfs: clean up vfs...
3557
3558
3559
  out:
  	if (target)
  		mutex_unlock(&target->i_mutex);
1d2ef5901   Al Viro   restore pinning t...
3560
  	dput(new_dentry);
e31e14ec3   Stephen Smalley   [PATCH] remove th...
3561
  	if (!error)
349457ccf   Mark Fasheh   [PATCH] Allow fil...
3562
3563
  		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  			d_move(old_dentry,new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3564
3565
  	return error;
  }
75c96f858   Adrian Bunk   [PATCH] make some...
3566
3567
  static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  			    struct inode *new_dir, struct dentry *new_dentry)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3568
  {
51892bbb5   Sage Weil   vfs: clean up vfs...
3569
  	struct inode *target = new_dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3570
3571
3572
3573
3574
3575
3576
  	int error;
  
  	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  	if (error)
  		return error;
  
  	dget(new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3577
  	if (target)
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3578
  		mutex_lock(&target->i_mutex);
51892bbb5   Sage Weil   vfs: clean up vfs...
3579
3580
  
  	error = -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3581
  	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
51892bbb5   Sage Weil   vfs: clean up vfs...
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
  		goto out;
  
  	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  	if (error)
  		goto out;
  
  	if (target)
  		dont_mount(new_dentry);
  	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  		d_move(old_dentry, new_dentry);
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3593
  	if (target)
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
3594
  		mutex_unlock(&target->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3595
3596
3597
3598
3599
3600
3601
3602
3603
  	dput(new_dentry);
  	return error;
  }
  
  int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  	       struct inode *new_dir, struct dentry *new_dentry)
  {
  	int error;
  	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
59b0df211   Eric Paris   fsnotify: use uns...
3604
  	const unsigned char *old_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3605
3606
3607
3608
3609
3610
3611
3612
3613
  
  	if (old_dentry->d_inode == new_dentry->d_inode)
   		return 0;
   
  	error = may_delete(old_dir, old_dentry, is_dir);
  	if (error)
  		return error;
  
  	if (!new_dentry->d_inode)
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
3614
  		error = may_create(new_dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3615
3616
3617
3618
  	else
  		error = may_delete(new_dir, new_dentry, is_dir);
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
3619
  	if (!old_dir->i_op->rename)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3620
  		return -EPERM;
0eeca2830   Robert Love   [PATCH] inotify
3621
  	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3622
3623
3624
3625
  	if (is_dir)
  		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  	else
  		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
123df2944   Al Viro   Lose the new_name...
3626
3627
  	if (!error)
  		fsnotify_move(old_dir, new_dir, old_name, is_dir,
5a190ae69   Al Viro   [PATCH] pass dent...
3628
  			      new_dentry->d_inode, old_dentry);
0eeca2830   Robert Love   [PATCH] inotify
3629
  	fsnotify_oldname_free(old_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3630
3631
  	return error;
  }
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
3632
3633
  SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  		int, newdfd, const char __user *, newname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3634
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
3635
3636
3637
  	struct dentry *old_dir, *new_dir;
  	struct dentry *old_dentry, *new_dentry;
  	struct dentry *trap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3638
  	struct nameidata oldnd, newnd;
91a27b2a7   Jeff Layton   vfs: define struc...
3639
3640
  	struct filename *from;
  	struct filename *to;
c6a942840   Jeff Layton   vfs: fix renameat...
3641
3642
  	unsigned int lookup_flags = 0;
  	bool should_retry = false;
2ad94ae65   Al Viro   [PATCH] new (loca...
3643
  	int error;
c6a942840   Jeff Layton   vfs: fix renameat...
3644
3645
  retry:
  	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
3646
3647
  	if (IS_ERR(from)) {
  		error = PTR_ERR(from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3648
  		goto exit;
91a27b2a7   Jeff Layton   vfs: define struc...
3649
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3650

c6a942840   Jeff Layton   vfs: fix renameat...
3651
  	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
91a27b2a7   Jeff Layton   vfs: define struc...
3652
3653
  	if (IS_ERR(to)) {
  		error = PTR_ERR(to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3654
  		goto exit1;
91a27b2a7   Jeff Layton   vfs: define struc...
3655
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3656
3657
  
  	error = -EXDEV;
4ac913785   Jan Blunck   Embed a struct pa...
3658
  	if (oldnd.path.mnt != newnd.path.mnt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3659
  		goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
3660
  	old_dir = oldnd.path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3661
3662
3663
  	error = -EBUSY;
  	if (oldnd.last_type != LAST_NORM)
  		goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
3664
  	new_dir = newnd.path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3665
3666
  	if (newnd.last_type != LAST_NORM)
  		goto exit2;
c30dabfe5   Jan Kara   fs: Push mnt_want...
3667
3668
3669
  	error = mnt_want_write(oldnd.path.mnt);
  	if (error)
  		goto exit2;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3670
3671
  	oldnd.flags &= ~LOOKUP_PARENT;
  	newnd.flags &= ~LOOKUP_PARENT;
4e9ed2f85   OGAWA Hirofumi   [PATCH vfs-2.6 6/...
3672
  	newnd.flags |= LOOKUP_RENAME_TARGET;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3673

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3674
  	trap = lock_rename(new_dir, old_dir);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
3675
  	old_dentry = lookup_hash(&oldnd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
  	error = PTR_ERR(old_dentry);
  	if (IS_ERR(old_dentry))
  		goto exit3;
  	/* source must exist */
  	error = -ENOENT;
  	if (!old_dentry->d_inode)
  		goto exit4;
  	/* unless the source is a directory trailing slashes give -ENOTDIR */
  	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  		error = -ENOTDIR;
  		if (oldnd.last.name[oldnd.last.len])
  			goto exit4;
  		if (newnd.last.name[newnd.last.len])
  			goto exit4;
  	}
  	/* source should not be ancestor of target */
  	error = -EINVAL;
  	if (old_dentry == trap)
  		goto exit4;
49705b774   Christoph Hellwig   [PATCH] sanitize ...
3695
  	new_dentry = lookup_hash(&newnd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3696
3697
3698
3699
3700
3701
3702
  	error = PTR_ERR(new_dentry);
  	if (IS_ERR(new_dentry))
  		goto exit4;
  	/* target should not be an ancestor of source */
  	error = -ENOTEMPTY;
  	if (new_dentry == trap)
  		goto exit5;
be6d3e56a   Kentaro Takeda   introduce new LSM...
3703
3704
3705
  	error = security_path_rename(&oldnd.path, old_dentry,
  				     &newnd.path, new_dentry);
  	if (error)
c30dabfe5   Jan Kara   fs: Push mnt_want...
3706
  		goto exit5;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3707
3708
3709
3710
3711
3712
3713
3714
  	error = vfs_rename(old_dir->d_inode, old_dentry,
  				   new_dir->d_inode, new_dentry);
  exit5:
  	dput(new_dentry);
  exit4:
  	dput(old_dentry);
  exit3:
  	unlock_rename(new_dir, old_dir);
c30dabfe5   Jan Kara   fs: Push mnt_want...
3715
  	mnt_drop_write(oldnd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3716
  exit2:
c6a942840   Jeff Layton   vfs: fix renameat...
3717
3718
  	if (retry_estale(error, lookup_flags))
  		should_retry = true;
1d957f9bf   Jan Blunck   Introduce path_put()
3719
  	path_put(&newnd.path);
2ad94ae65   Al Viro   [PATCH] new (loca...
3720
  	putname(to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3721
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
3722
  	path_put(&oldnd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3723
  	putname(from);
c6a942840   Jeff Layton   vfs: fix renameat...
3724
3725
3726
3727
3728
  	if (should_retry) {
  		should_retry = false;
  		lookup_flags |= LOOKUP_REVAL;
  		goto retry;
  	}
2ad94ae65   Al Viro   [PATCH] new (loca...
3729
  exit:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3730
3731
  	return error;
  }
a26eab240   Heiko Carstens   [CVE-2009-0029] S...
3732
  SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3733
3734
3735
  {
  	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
  int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  {
  	int len;
  
  	len = PTR_ERR(link);
  	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;
  }
  
  /*
   * 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...
3761
  	void *cookie;
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
3762
  	int res;
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3763

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3764
  	nd.depth = 0;
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3765
  	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
3766
3767
3768
3769
3770
3771
3772
  	if (IS_ERR(cookie))
  		return PTR_ERR(cookie);
  
  	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  	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
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
  }
  
  int vfs_follow_link(struct nameidata *nd, const char *link)
  {
  	return __vfs_follow_link(nd, link);
  }
  
  /* get the link contents into pagecache */
  static char *page_getlink(struct dentry * dentry, struct page **ppage)
  {
ebd09abbd   Duane Griffin   vfs: ensure page ...
3783
3784
  	char *kaddr;
  	struct page *page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3785
  	struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b185   Pekka Enberg   [PATCH] read_mapp...
3786
  	page = read_mapping_page(mapping, 0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3787
  	if (IS_ERR(page))
6fe6900e1   Nick Piggin   mm: make read_cac...
3788
  		return (char*)page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3789
  	*ppage = page;
ebd09abbd   Duane Griffin   vfs: ensure page ...
3790
3791
3792
  	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
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
  }
  
  int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  {
  	struct page *page = NULL;
  	char *s = page_getlink(dentry, &page);
  	int res = vfs_readlink(dentry,buffer,buflen,s);
  	if (page) {
  		kunmap(page);
  		page_cache_release(page);
  	}
  	return res;
  }
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3806
  void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3807
  {
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3808
  	struct page *page = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3809
  	nd_set_link(nd, page_getlink(dentry, &page));
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3810
  	return page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3811
  }
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3812
  void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3813
  {
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3814
3815
3816
  	struct page *page = cookie;
  
  	if (page) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3817
3818
  		kunmap(page);
  		page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3819
3820
  	}
  }
54566b2c1   Nick Piggin   fs: symlink write...
3821
3822
3823
3824
  /*
   * 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
3825
3826
  {
  	struct address_space *mapping = inode->i_mapping;
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3827
  	struct page *page;
afddba49d   Nick Piggin   fs: introduce wri...
3828
  	void *fsdata;
beb497ab4   Dmitriy Monakhov   [PATCH] __page_sy...
3829
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3830
  	char *kaddr;
54566b2c1   Nick Piggin   fs: symlink write...
3831
3832
3833
  	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  	if (nofs)
  		flags |= AOP_FLAG_NOFS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3834

7e53cac41   NeilBrown   [PATCH] Honour AO...
3835
  retry:
afddba49d   Nick Piggin   fs: introduce wri...
3836
  	err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c1   Nick Piggin   fs: symlink write...
3837
  				flags, &page, &fsdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3838
  	if (err)
afddba49d   Nick Piggin   fs: introduce wri...
3839
  		goto fail;
e8e3c3d66   Cong Wang   fs: remove the se...
3840
  	kaddr = kmap_atomic(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3841
  	memcpy(kaddr, symname, len-1);
e8e3c3d66   Cong Wang   fs: remove the se...
3842
  	kunmap_atomic(kaddr);
afddba49d   Nick Piggin   fs: introduce wri...
3843
3844
3845
  
  	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  							page, fsdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3846
3847
  	if (err < 0)
  		goto fail;
afddba49d   Nick Piggin   fs: introduce wri...
3848
3849
  	if (err < len-1)
  		goto retry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3850
3851
  	mark_inode_dirty(inode);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3852
3853
3854
  fail:
  	return err;
  }
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3855
3856
3857
  int page_symlink(struct inode *inode, const char *symname, int len)
  {
  	return __page_symlink(inode, symname, len,
54566b2c1   Nick Piggin   fs: symlink write...
3858
  			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3859
  }
92e1d5be9   Arjan van de Ven   [PATCH] mark stru...
3860
  const struct inode_operations page_symlink_inode_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3861
3862
3863
3864
  	.readlink	= generic_readlink,
  	.follow_link	= page_follow_link_light,
  	.put_link	= page_put_link,
  };
2d8f30380   Al Viro   [PATCH] sanitize ...
3865
  EXPORT_SYMBOL(user_path_at);
cc53ce53c   David Howells   Add a dentry op t...
3866
  EXPORT_SYMBOL(follow_down_one);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3867
3868
  EXPORT_SYMBOL(follow_down);
  EXPORT_SYMBOL(follow_up);
f6d2ac5ca   Al Viro   namei.c: fix BS c...
3869
  EXPORT_SYMBOL(get_write_access); /* nfsd */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3870
  EXPORT_SYMBOL(lock_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3871
3872
3873
3874
  EXPORT_SYMBOL(lookup_one_len);
  EXPORT_SYMBOL(page_follow_link_light);
  EXPORT_SYMBOL(page_put_link);
  EXPORT_SYMBOL(page_readlink);
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3875
  EXPORT_SYMBOL(__page_symlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3876
3877
  EXPORT_SYMBOL(page_symlink);
  EXPORT_SYMBOL(page_symlink_inode_operations);
d18114657   Al Viro   [PATCH] new helpe...
3878
  EXPORT_SYMBOL(kern_path);
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
3879
  EXPORT_SYMBOL(vfs_path_lookup);
f419a2e3b   Al Viro   [PATCH] kill name...
3880
  EXPORT_SYMBOL(inode_permission);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
  EXPORT_SYMBOL(unlock_rename);
  EXPORT_SYMBOL(vfs_create);
  EXPORT_SYMBOL(vfs_follow_link);
  EXPORT_SYMBOL(vfs_link);
  EXPORT_SYMBOL(vfs_mkdir);
  EXPORT_SYMBOL(vfs_mknod);
  EXPORT_SYMBOL(generic_permission);
  EXPORT_SYMBOL(vfs_readlink);
  EXPORT_SYMBOL(vfs_rename);
  EXPORT_SYMBOL(vfs_rmdir);
  EXPORT_SYMBOL(vfs_symlink);
  EXPORT_SYMBOL(vfs_unlink);
  EXPORT_SYMBOL(dentry_unhash);
  EXPORT_SYMBOL(generic_readlink);