Blame view

fs/namei.c 82.6 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
18
19
20
21
  /*
   *  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>
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include <linux/namei.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #include <linux/pagemap.h>
0eeca2830   Robert Love   [PATCH] inotify
23
  #include <linux/fsnotify.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
  #include <linux/personality.h>
  #include <linux/security.h>
6146f0d5e   Mimi Zohar   integrity: IMA hooks
26
  #include <linux/ima.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
  #include <linux/syscalls.h>
  #include <linux/mount.h>
  #include <linux/audit.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
30
  #include <linux/capability.h>
834f2a4a1   Trond Myklebust   VFS: Allow the fi...
31
  #include <linux/file.h>
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
32
  #include <linux/fcntl.h>
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
33
  #include <linux/device_cgroup.h>
5ad4e53bd   Al Viro   Get rid of indire...
34
  #include <linux/fs_struct.h>
e77819e57   Linus Torvalds   vfs: move ACL cac...
35
  #include <linux/posix_acl.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  #include <asm/uaccess.h>
e81e3f4dc   Eric Paris   fs: move get_empt...
37
  #include "internal.h"
c71053659   Al Viro   vfs: spread struc...
38
  #include "mount.h"
e81e3f4dc   Eric Paris   fs: move get_empt...
39

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
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
  /* [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...
74
   * the name is a symlink pointing to a non-existent name.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
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
   *
   * 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...
107
   * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
109
110
111
112
113
114
115
116
117
   * 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.
   */
858119e15   Arjan van de Ven   [PATCH] Unlinline...
118
  static int do_getname(const char __user *filename, char *page)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  {
  	int retval;
  	unsigned long len = PATH_MAX;
  
  	if (!segment_eq(get_fs(), KERNEL_DS)) {
  		if ((unsigned long) filename >= TASK_SIZE)
  			return -EFAULT;
  		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  			len = TASK_SIZE - (unsigned long) filename;
  	}
  
  	retval = strncpy_from_user(page, filename, len);
  	if (retval > 0) {
  		if (retval < len)
  			return 0;
  		return -ENAMETOOLONG;
  	} else if (!retval)
  		retval = -ENOENT;
  	return retval;
  }
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
139
  static char *getname_flags(const char __user *filename, int flags, int *empty)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
142
143
144
145
146
147
148
149
  {
  	char *tmp, *result;
  
  	result = ERR_PTR(-ENOMEM);
  	tmp = __getname();
  	if (tmp)  {
  		int retval = do_getname(filename, tmp);
  
  		result = tmp;
  		if (retval < 0) {
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
150
151
  			if (retval == -ENOENT && empty)
  				*empty = 1;
f52e0c113   Al Viro   New AT_... flag: ...
152
153
154
155
  			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
  				__putname(tmp);
  				result = ERR_PTR(retval);
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
  		}
  	}
  	audit_getname(result);
  	return result;
  }
f52e0c113   Al Viro   New AT_... flag: ...
161
162
  char *getname(const char __user * filename)
  {
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
163
  	return getname_flags(filename, 0, 0);
f52e0c113   Al Viro   New AT_... flag: ...
164
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
  #ifdef CONFIG_AUDITSYSCALL
  void putname(const char *name)
  {
5ac3a9c26   Al Viro   [PATCH] don't bot...
168
  	if (unlikely(!audit_dummy_context()))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
173
174
  		audit_putname(name);
  	else
  		__putname(name);
  }
  EXPORT_SYMBOL(putname);
  #endif
e77819e57   Linus Torvalds   vfs: move ACL cac...
175
176
  static int check_acl(struct inode *inode, int mask)
  {
84635d68b   Linus Torvalds   vfs: fix check_ac...
177
  #ifdef CONFIG_FS_POSIX_ACL
e77819e57   Linus Torvalds   vfs: move ACL cac...
178
  	struct posix_acl *acl;
e77819e57   Linus Torvalds   vfs: move ACL cac...
179
  	if (mask & MAY_NOT_BLOCK) {
3567866bf   Al Viro   RCUify freeing ac...
180
181
  		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  	        if (!acl)
e77819e57   Linus Torvalds   vfs: move ACL cac...
182
  	                return -EAGAIN;
3567866bf   Al Viro   RCUify freeing ac...
183
184
185
  		/* no ->get_acl() calls in RCU mode... */
  		if (acl == ACL_NOT_CACHED)
  			return -ECHILD;
206b1d09a   Ari Savolainen   Fix POSIX ACL per...
186
  	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
e77819e57   Linus Torvalds   vfs: move ACL cac...
187
188
189
190
191
  	}
  
  	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
  
  	/*
4e34e719e   Christoph Hellwig   fs: take the ACL ...
192
193
194
  	 * 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...
195
  	 *
4e34e719e   Christoph Hellwig   fs: take the ACL ...
196
197
  	 * 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...
198
199
  	 */
  	if (acl == ACL_NOT_CACHED) {
4e34e719e   Christoph Hellwig   fs: take the ACL ...
200
201
202
203
204
205
206
207
  	        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...
208
209
210
211
212
213
214
  	}
  
  	if (acl) {
  	        int error = posix_acl_permission(inode, acl, mask);
  	        posix_acl_release(acl);
  	        return error;
  	}
84635d68b   Linus Torvalds   vfs: fix check_ac...
215
  #endif
e77819e57   Linus Torvalds   vfs: move ACL cac...
216
217
218
  
  	return -EAGAIN;
  }
5909ccaa3   Linus Torvalds   Make 'check_acl()...
219
  /*
948409c74   Andreas Gruenbacher   vfs: add a commen...
220
   * This does the basic permission checking
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
   */
7e40145eb   Al Viro   ->permission() sa...
222
  static int acl_permission_check(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
  {
26cf46be9   Linus Torvalds   vfs: micro-optimi...
224
  	unsigned int mode = inode->i_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225

e795b7179   Serge E. Hallyn   userns: userns: c...
226
227
  	if (current_user_ns() != inode_userns(inode))
  		goto other_perms;
14067ff53   Linus Torvalds   vfs: make gcc gen...
228
  	if (likely(current_fsuid() == inode->i_uid))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
  		mode >>= 6;
  	else {
e77819e57   Linus Torvalds   vfs: move ACL cac...
231
  		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
7e40145eb   Al Viro   ->permission() sa...
232
  			int error = check_acl(inode, mask);
b74c79e99   Nick Piggin   fs: provide rcu-w...
233
234
  			if (error != -EAGAIN)
  				return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
236
237
238
239
  		}
  
  		if (in_group_p(inode->i_gid))
  			mode >>= 3;
  	}
e795b7179   Serge E. Hallyn   userns: userns: c...
240
  other_perms:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
  	/*
  	 * If the DACs are ok we don't need any capability check.
  	 */
9c2c70392   Al Viro   ->permission() sa...
244
  	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
  		return 0;
5909ccaa3   Linus Torvalds   Make 'check_acl()...
246
247
248
249
  	return -EACCES;
  }
  
  /**
b74c79e99   Nick Piggin   fs: provide rcu-w...
250
   * generic_permission -  check for access rights on a Posix-like filesystem
5909ccaa3   Linus Torvalds   Make 'check_acl()...
251
   * @inode:	inode to check access rights for
8fd90c8d1   Andreas Gruenbacher   vfs: indicate tha...
252
   * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
5909ccaa3   Linus Torvalds   Make 'check_acl()...
253
254
255
256
   *
   * 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...
257
258
259
260
261
   * 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()...
262
   */
2830ba7f3   Al Viro   ->permission() sa...
263
  int generic_permission(struct inode *inode, int mask)
5909ccaa3   Linus Torvalds   Make 'check_acl()...
264
265
266
267
  {
  	int ret;
  
  	/*
948409c74   Andreas Gruenbacher   vfs: add a commen...
268
  	 * Do the basic permission checks.
5909ccaa3   Linus Torvalds   Make 'check_acl()...
269
  	 */
7e40145eb   Al Viro   ->permission() sa...
270
  	ret = acl_permission_check(inode, mask);
5909ccaa3   Linus Torvalds   Make 'check_acl()...
271
272
  	if (ret != -EACCES)
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273

d594e7ec4   Al Viro   massage generic_p...
274
275
276
277
278
279
280
281
282
  	if (S_ISDIR(inode->i_mode)) {
  		/* DACs are overridable for directories */
  		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
  			return 0;
  		if (!(mask & MAY_WRITE))
  			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
  				return 0;
  		return -EACCES;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
  	/*
  	 * Read/write DACs are always overridable.
d594e7ec4   Al Viro   massage generic_p...
285
286
  	 * Executable DACs are overridable when there is
  	 * at least one exec bit set.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
  	 */
d594e7ec4   Al Viro   massage generic_p...
288
  	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
e795b7179   Serge E. Hallyn   userns: userns: c...
289
  		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
291
292
293
294
  			return 0;
  
  	/*
  	 * Searching includes executable on directories, else just read.
  	 */
7ea660014   Serge E. Hallyn   generic_permissio...
295
  	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec4   Al Viro   massage generic_p...
296
  	if (mask == MAY_READ)
e795b7179   Serge E. Hallyn   userns: userns: c...
297
  		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
300
301
  			return 0;
  
  	return -EACCES;
  }
3ddcd0569   Linus Torvalds   vfs: optimize ino...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  /*
   * 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
321
322
323
  /**
   * inode_permission  -  check for access rights to a given inode
   * @inode:	inode to check permission on
8fd90c8d1   Andreas Gruenbacher   vfs: indicate tha...
324
   * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
cb23beb55   Christoph Hellwig   kill vfs_permission
325
326
327
328
329
   *
   * Used to check for read/write/execute permissions on an inode.
   * We use "fsuid" for this, letting us set arbitrary permissions
   * for filesystem access without changing the "normal" uids which
   * are used for other things.
948409c74   Andreas Gruenbacher   vfs: add a commen...
330
331
   *
   * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
cb23beb55   Christoph Hellwig   kill vfs_permission
332
   */
f419a2e3b   Al Viro   [PATCH] kill name...
333
  int inode_permission(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
  {
e6305c43e   Al Viro   [PATCH] sanitize ...
335
  	int retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336

3ddcd0569   Linus Torvalds   vfs: optimize ino...
337
  	if (unlikely(mask & MAY_WRITE)) {
22590e41c   Miklos Szeredi   fix execute check...
338
  		umode_t mode = inode->i_mode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  
  		/*
  		 * Nobody gets write access to a read-only fs.
  		 */
  		if (IS_RDONLY(inode) &&
  		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  			return -EROFS;
  
  		/*
  		 * Nobody gets write access to an immutable file.
  		 */
  		if (IS_IMMUTABLE(inode))
  			return -EACCES;
  	}
3ddcd0569   Linus Torvalds   vfs: optimize ino...
353
  	retval = do_inode_permission(inode, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
  	if (retval)
  		return retval;
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
356
357
358
  	retval = devcgroup_inode_permission(inode, mask);
  	if (retval)
  		return retval;
d09ca7397   Eric Paris   security: make LS...
359
  	return security_inode_permission(inode, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360
  }
f4d6ff89d   Al Viro   move exec_permiss...
361
  /**
5dd784d04   Jan Blunck   Introduce path_get()
362
363
364
365
366
367
368
369
370
371
372
373
374
   * 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.
   */
  void path_get(struct path *path)
  {
  	mntget(path->mnt);
  	dget(path->dentry);
  }
  EXPORT_SYMBOL(path_get);
  
  /**
1d957f9bf   Jan Blunck   Introduce path_put()
375
376
377
378
379
380
   * 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.
   */
  void path_put(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
  {
1d957f9bf   Jan Blunck   Introduce path_put()
382
383
  	dput(path->dentry);
  	mntput(path->mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
  }
1d957f9bf   Jan Blunck   Introduce path_put()
385
  EXPORT_SYMBOL(path_put);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386

19660af73   Al Viro   consolidate namei...
387
  /*
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
388
   * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af73   Al Viro   consolidate namei...
389
390
391
392
393
394
395
   * 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 ...
396
   */
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
397
398
  
  /**
19660af73   Al Viro   consolidate namei...
399
400
401
   * 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...
402
   * Returns: 0 on success, -ECHILD on failure
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
403
   *
19660af73   Al Viro   consolidate namei...
404
405
406
   * 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 ...
407
   */
19660af73   Al Viro   consolidate namei...
408
  static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
409
410
411
  {
  	struct fs_struct *fs = current->fs;
  	struct dentry *parent = nd->path.dentry;
5b6ca027d   Al Viro   reduce vfs_path_l...
412
  	int want_root = 0;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
413
414
  
  	BUG_ON(!(nd->flags & LOOKUP_RCU));
5b6ca027d   Al Viro   reduce vfs_path_l...
415
416
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  		want_root = 1;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
417
418
419
420
421
422
  		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...
423
424
425
426
427
  	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...
428
429
  		if (dentry->d_parent != parent)
  			goto err_parent;
19660af73   Al Viro   consolidate namei...
430
431
432
433
434
435
436
437
438
439
440
441
442
443
  		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 ...
444
  	spin_unlock(&parent->d_lock);
5b6ca027d   Al Viro   reduce vfs_path_l...
445
  	if (want_root) {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
446
447
448
449
450
451
452
453
454
  		path_get(&nd->root);
  		spin_unlock(&fs->lock);
  	}
  	mntget(nd->path.mnt);
  
  	rcu_read_unlock();
  	br_read_unlock(vfsmount_lock);
  	nd->flags &= ~LOOKUP_RCU;
  	return 0;
19660af73   Al Viro   consolidate namei...
455
456
  
  err_child:
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
457
  	spin_unlock(&dentry->d_lock);
19660af73   Al Viro   consolidate namei...
458
  err_parent:
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
459
460
  	spin_unlock(&parent->d_lock);
  err_root:
5b6ca027d   Al Viro   reduce vfs_path_l...
461
  	if (want_root)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
462
463
464
  		spin_unlock(&fs->lock);
  	return -ECHILD;
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
465
  /**
834f2a4a1   Trond Myklebust   VFS: Allow the fi...
466
467
468
469
470
   * release_open_intent - free up open intent resources
   * @nd: pointer to nameidata
   */
  void release_open_intent(struct nameidata *nd)
  {
2dab59744   Linus Torvalds   Fix possible filp...
471
472
473
474
475
476
477
478
  	struct file *file = nd->intent.open.file;
  
  	if (file && !IS_ERR(file)) {
  		if (file->f_path.dentry == NULL)
  			put_filp(file);
  		else
  			fput(file);
  	}
834f2a4a1   Trond Myklebust   VFS: Allow the fi...
479
  }
f60aef7ec   Al Viro   drop out of RCU i...
480
  static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
34286d666   Nick Piggin   fs: rcu-walk awar...
481
  {
f60aef7ec   Al Viro   drop out of RCU i...
482
  	return dentry->d_op->d_revalidate(dentry, nd);
34286d666   Nick Piggin   fs: rcu-walk awar...
483
  }
9f1fafee9   Al Viro   merge handle_reva...
484
485
486
  /**
   * complete_walk - successful completion of path walk
   * @nd:  pointer nameidata
39159de2a   Jeff Layton   vfs: force reval ...
487
   *
9f1fafee9   Al Viro   merge handle_reva...
488
489
490
491
492
   * 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 ...
493
   */
9f1fafee9   Al Viro   merge handle_reva...
494
  static int complete_walk(struct nameidata *nd)
39159de2a   Jeff Layton   vfs: force reval ...
495
  {
16c2cd717   Al Viro   untangle the "nee...
496
  	struct dentry *dentry = nd->path.dentry;
39159de2a   Jeff Layton   vfs: force reval ...
497
  	int status;
39159de2a   Jeff Layton   vfs: force reval ...
498

9f1fafee9   Al Viro   merge handle_reva...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  	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);
  			rcu_read_unlock();
  			br_read_unlock(vfsmount_lock);
  			return -ECHILD;
  		}
  		BUG_ON(nd->inode != dentry->d_inode);
  		spin_unlock(&dentry->d_lock);
  		mntget(nd->path.mnt);
  		rcu_read_unlock();
  		br_read_unlock(vfsmount_lock);
  	}
16c2cd717   Al Viro   untangle the "nee...
516
517
518
519
  	if (likely(!(nd->flags & LOOKUP_JUMPED)))
  		return 0;
  
  	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
39159de2a   Jeff Layton   vfs: force reval ...
520
  		return 0;
16c2cd717   Al Viro   untangle the "nee...
521
522
523
524
  	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  		return 0;
  
  	/* Note: we do not d_invalidate() */
34286d666   Nick Piggin   fs: rcu-walk awar...
525
  	status = d_revalidate(dentry, nd);
39159de2a   Jeff Layton   vfs: force reval ...
526
527
  	if (status > 0)
  		return 0;
16c2cd717   Al Viro   untangle the "nee...
528
  	if (!status)
39159de2a   Jeff Layton   vfs: force reval ...
529
  		status = -ESTALE;
16c2cd717   Al Viro   untangle the "nee...
530

9f1fafee9   Al Viro   merge handle_reva...
531
  	path_put(&nd->path);
39159de2a   Jeff Layton   vfs: force reval ...
532
533
  	return status;
  }
2a7378711   Al Viro   Cache root in nam...
534
535
  static __always_inline void set_root(struct nameidata *nd)
  {
f7ad3c6be   Miklos Szeredi   vfs: add helpers ...
536
537
  	if (!nd->root.mnt)
  		get_fs_root(current->fs, &nd->root);
2a7378711   Al Viro   Cache root in nam...
538
  }
6de88d729   Al Viro   kill __link_path_...
539
  static int link_path_walk(const char *, struct nameidata *);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
540
541
542
543
  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...
544
545
546
547
548
  		unsigned seq;
  
  		do {
  			seq = read_seqcount_begin(&fs->seq);
  			nd->root = fs->root;
c1530019e   Tim Chen   vfs: Fix absolute...
549
  			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
c28cc3646   Nick Piggin   fs: fs_struct use...
550
  		} while (read_seqcount_retry(&fs->seq, seq));
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
551
552
  	}
  }
f16623569   Arjan van de Ven   [PATCH] Mark some...
553
  static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
554
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
555
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
558
559
  	if (IS_ERR(link))
  		goto fail;
  
  	if (*link == '/') {
2a7378711   Al Viro   Cache root in nam...
560
  		set_root(nd);
1d957f9bf   Jan Blunck   Introduce path_put()
561
  		path_put(&nd->path);
2a7378711   Al Viro   Cache root in nam...
562
563
  		nd->path = nd->root;
  		path_get(&nd->root);
16c2cd717   Al Viro   untangle the "nee...
564
  		nd->flags |= LOOKUP_JUMPED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
565
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
566
  	nd->inode = nd->path.dentry->d_inode;
b4091d5f6   Christoph Hellwig   kill walk_init_root
567

31e6b01f4   Nick Piggin   fs: rcu-walk for ...
568
569
  	ret = link_path_walk(link, nd);
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
570
  fail:
1d957f9bf   Jan Blunck   Introduce path_put()
571
  	path_put(&nd->path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
573
  	return PTR_ERR(link);
  }
1d957f9bf   Jan Blunck   Introduce path_put()
574
  static void path_put_conditional(struct path *path, struct nameidata *nd)
051d38125   Ian Kent   [PATCH] autofs4: ...
575
576
  {
  	dput(path->dentry);
4ac913785   Jan Blunck   Embed a struct pa...
577
  	if (path->mnt != nd->path.mnt)
051d38125   Ian Kent   [PATCH] autofs4: ...
578
579
  		mntput(path->mnt);
  }
7b9337aaf   Nick Piggin   fs: namei fix ->p...
580
581
  static inline void path_to_nameidata(const struct path *path,
  					struct nameidata *nd)
051d38125   Ian Kent   [PATCH] autofs4: ...
582
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
583
584
585
586
  	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 ...
587
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
588
  	nd->path.mnt = path->mnt;
4ac913785   Jan Blunck   Embed a struct pa...
589
  	nd->path.dentry = path->dentry;
051d38125   Ian Kent   [PATCH] autofs4: ...
590
  }
574197e0d   Al Viro   tidy the trailing...
591
592
593
594
595
596
597
  static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  {
  	struct inode *inode = link->dentry->d_inode;
  	if (!IS_ERR(cookie) && inode->i_op->put_link)
  		inode->i_op->put_link(link->dentry, nd, cookie);
  	path_put(link);
  }
def4af30c   Al Viro   Get rid of symlin...
598
  static __always_inline int
574197e0d   Al Viro   tidy the trailing...
599
  follow_link(struct path *link, struct nameidata *nd, void **p)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
601
  {
  	int error;
7b9337aaf   Nick Piggin   fs: namei fix ->p...
602
  	struct dentry *dentry = link->dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
603

844a39179   Al Viro   nothing in do_fol...
604
  	BUG_ON(nd->flags & LOOKUP_RCU);
0e794589e   Al Viro   fix follow_link()...
605
606
  	if (link->mnt == nd->path.mnt)
  		mntget(link->mnt);
574197e0d   Al Viro   tidy the trailing...
607
608
  	if (unlikely(current->total_link_count >= 40)) {
  		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
574197e0d   Al Viro   tidy the trailing...
609
610
611
612
613
  		path_put(&nd->path);
  		return -ELOOP;
  	}
  	cond_resched();
  	current->total_link_count++;
7b9337aaf   Nick Piggin   fs: namei fix ->p...
614
  	touch_atime(link->mnt, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
615
  	nd_set_link(nd, NULL);
cd4e91d3b   Al Viro   [PATCH] namei fix...
616

36f3b4f69   Al Viro   pull security_ino...
617
618
619
620
621
622
  	error = security_inode_follow_link(link->dentry, nd);
  	if (error) {
  		*p = ERR_PTR(error); /* no ->put_link(), please */
  		path_put(&nd->path);
  		return error;
  	}
86acdca1b   Al Viro   fix autofs/afs/et...
623
  	nd->last_type = LAST_BIND;
def4af30c   Al Viro   Get rid of symlin...
624
625
626
  	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
  	error = PTR_ERR(*p);
  	if (!IS_ERR(*p)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
  		char *s = nd_get_link(nd);
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
628
  		error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
630
  		if (s)
  			error = __vfs_follow_link(nd, s);
bcda76524   Al Viro   Allow O_PATH for ...
631
  		else if (nd->last_type == LAST_BIND) {
16c2cd717   Al Viro   untangle the "nee...
632
  			nd->flags |= LOOKUP_JUMPED;
b21041d0f   Al Viro   update nd->inode ...
633
634
  			nd->inode = nd->path.dentry->d_inode;
  			if (nd->inode->i_op->follow_link) {
bcda76524   Al Viro   Allow O_PATH for ...
635
636
637
638
639
  				/* stepped on a _really_ weird one */
  				path_put(&nd->path);
  				error = -ELOOP;
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
  	return error;
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
643
644
  static int follow_up_rcu(struct path *path)
  {
0714a5338   Al Viro   vfs: now it can b...
645
646
  	struct mount *mnt = real_mount(path->mnt);
  	struct mount *parent;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
647
  	struct dentry *mountpoint;
0714a5338   Al Viro   vfs: now it can b...
648
649
  	parent = mnt->mnt_parent;
  	if (&parent->mnt == path->mnt)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
650
  		return 0;
a73324da7   Al Viro   vfs: move mnt_mou...
651
  	mountpoint = mnt->mnt_mountpoint;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
652
  	path->dentry = mountpoint;
0714a5338   Al Viro   vfs: now it can b...
653
  	path->mnt = &parent->mnt;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
654
655
  	return 1;
  }
bab77ebf5   Al Viro   switch follow_up(...
656
  int follow_up(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
  {
0714a5338   Al Viro   vfs: now it can b...
658
659
  	struct mount *mnt = real_mount(path->mnt);
  	struct mount *parent;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  	struct dentry *mountpoint;
99b7db7b8   Nick Piggin   fs: brlock vfsmou...
661
662
  
  	br_read_lock(vfsmount_lock);
0714a5338   Al Viro   vfs: now it can b...
663
664
  	parent = mnt->mnt_parent;
  	if (&parent->mnt == path->mnt) {
99b7db7b8   Nick Piggin   fs: brlock vfsmou...
665
  		br_read_unlock(vfsmount_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
667
  		return 0;
  	}
0714a5338   Al Viro   vfs: now it can b...
668
  	mntget(&parent->mnt);
a73324da7   Al Viro   vfs: move mnt_mou...
669
  	mountpoint = dget(mnt->mnt_mountpoint);
99b7db7b8   Nick Piggin   fs: brlock vfsmou...
670
  	br_read_unlock(vfsmount_lock);
bab77ebf5   Al Viro   switch follow_up(...
671
672
673
  	dput(path->dentry);
  	path->dentry = mountpoint;
  	mntput(path->mnt);
0714a5338   Al Viro   vfs: now it can b...
674
  	path->mnt = &parent->mnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675
676
  	return 1;
  }
b5c84bf6f   Nick Piggin   fs: dcache remove...
677
  /*
9875cf806   David Howells   Add a dentry op t...
678
679
680
   * 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
681
   */
9875cf806   David Howells   Add a dentry op t...
682
683
  static int follow_automount(struct path *path, unsigned flags,
  			    bool *need_mntput)
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
684
  {
9875cf806   David Howells   Add a dentry op t...
685
  	struct vfsmount *mnt;
ea5b778a8   David Howells   Unexport do_add_m...
686
  	int err;
9875cf806   David Howells   Add a dentry op t...
687
688
689
  
  	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  		return -EREMOTE;
0ec26fd06   Miklos Szeredi   vfs: automount sh...
690
691
692
693
694
695
696
697
698
699
  	/* 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...
700
  	 */
0ec26fd06   Miklos Szeredi   vfs: automount sh...
701
  	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
d94c177be   Linus Torvalds   vfs pathname look...
702
  		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
0ec26fd06   Miklos Szeredi   vfs: automount sh...
703
704
  	    path->dentry->d_inode)
  		return -EISDIR;
9875cf806   David Howells   Add a dentry op t...
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
  	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
720
  		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9875cf806   David Howells   Add a dentry op t...
721
722
  			return -EREMOTE;
  		return PTR_ERR(mnt);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
723
  	}
ea5b778a8   David Howells   Unexport do_add_m...
724

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

8aef18845   Al Viro   VFS: Fix vfsmount...
728
729
730
731
732
  	if (!*need_mntput) {
  		/* lock_mount() may release path->mnt on error */
  		mntget(path->mnt);
  		*need_mntput = true;
  	}
19a167af7   Al Viro   Take the completi...
733
  	err = finish_automount(mnt, path);
9875cf806   David Howells   Add a dentry op t...
734

ea5b778a8   David Howells   Unexport do_add_m...
735
736
737
  	switch (err) {
  	case -EBUSY:
  		/* Someone else made a mount here whilst we were busy */
19a167af7   Al Viro   Take the completi...
738
  		return 0;
ea5b778a8   David Howells   Unexport do_add_m...
739
  	case 0:
8aef18845   Al Viro   VFS: Fix vfsmount...
740
  		path_put(path);
ea5b778a8   David Howells   Unexport do_add_m...
741
742
  		path->mnt = mnt;
  		path->dentry = dget(mnt->mnt_root);
ea5b778a8   David Howells   Unexport do_add_m...
743
  		return 0;
19a167af7   Al Viro   Take the completi...
744
745
  	default:
  		return err;
ea5b778a8   David Howells   Unexport do_add_m...
746
  	}
19a167af7   Al Viro   Take the completi...
747

463ffb2e9   Al Viro   [PATCH] namei fix...
748
  }
9875cf806   David Howells   Add a dentry op t...
749
750
  /*
   * Handle a dentry that is managed in some way.
cc53ce53c   David Howells   Add a dentry op t...
751
   * - Flagged for transit management (autofs)
9875cf806   David Howells   Add a dentry op t...
752
753
754
755
756
757
758
759
   * - 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
760
  {
8aef18845   Al Viro   VFS: Fix vfsmount...
761
  	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9875cf806   David Howells   Add a dentry op t...
762
763
  	unsigned managed;
  	bool need_mntput = false;
8aef18845   Al Viro   VFS: Fix vfsmount...
764
  	int ret = 0;
9875cf806   David Howells   Add a dentry op t...
765
766
767
768
769
770
771
  
  	/* 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...
772
773
774
775
776
  		/* 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...
777
  			ret = path->dentry->d_op->d_manage(path->dentry, false);
cc53ce53c   David Howells   Add a dentry op t...
778
  			if (ret < 0)
8aef18845   Al Viro   VFS: Fix vfsmount...
779
  				break;
cc53ce53c   David Howells   Add a dentry op t...
780
  		}
9875cf806   David Howells   Add a dentry op t...
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
  		/* 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...
804
  				break;
9875cf806   David Howells   Add a dentry op t...
805
806
807
808
809
  			continue;
  		}
  
  		/* We didn't change the current path point */
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
  	}
8aef18845   Al Viro   VFS: Fix vfsmount...
811
812
813
814
815
  
  	if (need_mntput && path->mnt == mnt)
  		mntput(path->mnt);
  	if (ret == -EISDIR)
  		ret = 0;
a3fbbde70   Al Viro   VFS: we need to s...
816
  	return ret < 0 ? ret : need_mntput;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
817
  }
cc53ce53c   David Howells   Add a dentry op t...
818
  int follow_down_one(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
819
820
  {
  	struct vfsmount *mounted;
1c755af4d   Al Viro   switch lookup_mnt()
821
  	mounted = lookup_mnt(path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
822
  	if (mounted) {
9393bd07c   Al Viro   switch follow_down()
823
824
825
826
  		dput(path->dentry);
  		mntput(path->mnt);
  		path->mnt = mounted;
  		path->dentry = dget(mounted->mnt_root);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
828
829
830
  		return 1;
  	}
  	return 0;
  }
62a7375e5   Ian Kent   vfs - check non-m...
831
832
833
834
835
  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...
836
  /*
287548e46   Al Viro   split __follow_mo...
837
838
   * 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...
839
840
   */
  static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
287548e46   Al Viro   split __follow_mo...
841
  			       struct inode **inode)
9875cf806   David Howells   Add a dentry op t...
842
  {
62a7375e5   Ian Kent   vfs - check non-m...
843
  	for (;;) {
c71053659   Al Viro   vfs: spread struc...
844
  		struct mount *mounted;
62a7375e5   Ian Kent   vfs - check non-m...
845
846
847
848
  		/*
  		 * Don't forget we might have a non-mountpoint managed dentry
  		 * that wants to block transit.
  		 */
287548e46   Al Viro   split __follow_mo...
849
  		if (unlikely(managed_dentry_might_block(path->dentry)))
ab90911ff   David Howells   Allow d_manage() ...
850
  			return false;
62a7375e5   Ian Kent   vfs - check non-m...
851
852
853
  
  		if (!d_mountpoint(path->dentry))
  			break;
9875cf806   David Howells   Add a dentry op t...
854
855
856
  		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  		if (!mounted)
  			break;
c71053659   Al Viro   vfs: spread struc...
857
858
  		path->mnt = &mounted->mnt;
  		path->dentry = mounted->mnt.mnt_root;
a3fbbde70   Al Viro   VFS: we need to s...
859
  		nd->flags |= LOOKUP_JUMPED;
9875cf806   David Howells   Add a dentry op t...
860
  		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
594302624   Linus Torvalds   vfs: fix race in ...
861
862
863
864
865
866
  		/*
  		 * 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...
867
  	}
9875cf806   David Howells   Add a dentry op t...
868
869
  	return true;
  }
dea393761   Al Viro   Trim excessive ar...
870
  static void follow_mount_rcu(struct nameidata *nd)
287548e46   Al Viro   split __follow_mo...
871
  {
dea393761   Al Viro   Trim excessive ar...
872
  	while (d_mountpoint(nd->path.dentry)) {
c71053659   Al Viro   vfs: spread struc...
873
  		struct mount *mounted;
dea393761   Al Viro   Trim excessive ar...
874
  		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
287548e46   Al Viro   split __follow_mo...
875
876
  		if (!mounted)
  			break;
c71053659   Al Viro   vfs: spread struc...
877
878
  		nd->path.mnt = &mounted->mnt;
  		nd->path.dentry = mounted->mnt.mnt_root;
dea393761   Al Viro   Trim excessive ar...
879
  		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
287548e46   Al Viro   split __follow_mo...
880
881
  	}
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
882
883
  static int follow_dotdot_rcu(struct nameidata *nd)
  {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
884
  	set_root_rcu(nd);
9875cf806   David Howells   Add a dentry op t...
885
  	while (1) {
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
886
887
888
889
890
891
892
893
894
895
896
  		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(...
897
  				goto failed;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
898
899
900
901
902
903
904
  			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 ...
905
  	}
dea393761   Al Viro   Trim excessive ar...
906
907
  	follow_mount_rcu(nd);
  	nd->inode = nd->path.dentry->d_inode;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
908
  	return 0;
ef7562d52   Al Viro   make handle_dots(...
909
910
911
  
  failed:
  	nd->flags &= ~LOOKUP_RCU;
5b6ca027d   Al Viro   reduce vfs_path_l...
912
913
  	if (!(nd->flags & LOOKUP_ROOT))
  		nd->root.mnt = NULL;
ef7562d52   Al Viro   make handle_dots(...
914
915
916
  	rcu_read_unlock();
  	br_read_unlock(vfsmount_lock);
  	return -ECHILD;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
917
  }
9875cf806   David Howells   Add a dentry op t...
918
  /*
cc53ce53c   David Howells   Add a dentry op t...
919
920
921
   * 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...
922
   */
7cc90cc3f   Al Viro   don't pass 'mount...
923
  int follow_down(struct path *path)
cc53ce53c   David Howells   Add a dentry op t...
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
  {
  	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() ...
943
  			ret = path->dentry->d_op->d_manage(
1aed3e420   Al Viro   lose 'mounting_he...
944
  				path->dentry, false);
cc53ce53c   David Howells   Add a dentry op t...
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
  			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...
968
969
970
971
972
973
974
975
976
977
978
979
980
981
   * 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 ...
982
  static void follow_dotdot(struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
983
  {
2a7378711   Al Viro   Cache root in nam...
984
  	set_root(nd);
e518ddb7b   Andreas Mohr   [PATCH] fs/namei....
985

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

2a7378711   Al Viro   Cache root in nam...
989
990
  		if (nd->path.dentry == nd->root.dentry &&
  		    nd->path.mnt == nd->root.mnt) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
991
992
  			break;
  		}
4ac913785   Jan Blunck   Embed a struct pa...
993
  		if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd708   Al Viro   Clean follow_dotd...
994
995
  			/* rare case of legitimate dget_parent()... */
  			nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
997
998
  			dput(old);
  			break;
  		}
3088dd708   Al Viro   Clean follow_dotd...
999
  		if (!follow_up(&nd->path))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1000
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1001
  	}
79ed02261   Al Viro   switch follow_mou...
1002
  	follow_mount(&nd->path);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1003
  	nd->inode = nd->path.dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1004
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1005
  /*
baa038907   Nick Piggin   fs: dentry alloca...
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
   * Allocate a dentry with name and parent, and perform a parent
   * directory ->lookup on it. Returns the new dentry, or ERR_PTR
   * on error. parent->d_inode->i_mutex must be held. d_lookup must
   * have verified that no child exists while under i_mutex.
   */
  static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  				struct qstr *name, struct nameidata *nd)
  {
  	struct inode *inode = parent->d_inode;
  	struct dentry *dentry;
  	struct dentry *old;
  
  	/* Don't create child dentry for a dead directory. */
  	if (unlikely(IS_DEADDIR(inode)))
  		return ERR_PTR(-ENOENT);
  
  	dentry = d_alloc(parent, name);
  	if (unlikely(!dentry))
  		return ERR_PTR(-ENOMEM);
  
  	old = inode->i_op->lookup(inode, dentry, nd);
  	if (unlikely(old)) {
  		dput(dentry);
  		dentry = old;
  	}
  	return dentry;
  }
  
  /*
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
   * We already have a dentry, but require a lookup to be performed on the parent
   * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
   * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
   * child exists while under i_mutex.
   */
  static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
  				     struct nameidata *nd)
  {
  	struct inode *inode = parent->d_inode;
  	struct dentry *old;
  
  	/* Don't create child dentry for a dead directory. */
  	if (unlikely(IS_DEADDIR(inode)))
  		return ERR_PTR(-ENOENT);
  
  	old = inode->i_op->lookup(inode, dentry, nd);
  	if (unlikely(old)) {
  		dput(dentry);
  		dentry = old;
  	}
  	return dentry;
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1059
1060
1061
1062
1063
   *  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.
   */
  static int do_lookup(struct nameidata *nd, struct qstr *name,
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1064
  			struct path *path, struct inode **inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1065
  {
4ac913785   Jan Blunck   Embed a struct pa...
1066
  	struct vfsmount *mnt = nd->path.mnt;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1067
  	struct dentry *dentry, *parent = nd->path.dentry;
5a18fff20   Al Viro   untangle do_lookup()
1068
1069
  	int need_reval = 1;
  	int status = 1;
9875cf806   David Howells   Add a dentry op t...
1070
  	int err;
3cac260ad   Al Viro   Take hash recalcu...
1071
  	/*
b04f784e5   Nick Piggin   fs: remove extra ...
1072
1073
1074
1075
  	 * 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 ...
1076
1077
  	if (nd->flags & LOOKUP_RCU) {
  		unsigned seq;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1078
1079
  		*inode = nd->inode;
  		dentry = __d_lookup_rcu(parent, name, &seq, inode);
5a18fff20   Al Viro   untangle do_lookup()
1080
1081
  		if (!dentry)
  			goto unlazy;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1082
1083
1084
  		/* Memory barrier in read_seqcount_begin of child is enough */
  		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  			return -ECHILD;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1085
  		nd->seq = seq;
5a18fff20   Al Viro   untangle do_lookup()
1086

24643087e   Al Viro   in do_lookup() sp...
1087
  		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
5a18fff20   Al Viro   untangle do_lookup()
1088
1089
1090
1091
1092
1093
  			status = d_revalidate(dentry, nd);
  			if (unlikely(status <= 0)) {
  				if (status != -ECHILD)
  					need_reval = 0;
  				goto unlazy;
  			}
24643087e   Al Viro   in do_lookup() sp...
1094
  		}
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1095
1096
  		if (unlikely(d_need_lookup(dentry)))
  			goto unlazy;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1097
1098
  		path->mnt = mnt;
  		path->dentry = dentry;
d6e9bd256   Al Viro   Lift the check fo...
1099
1100
1101
1102
1103
  		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()
1104
  unlazy:
19660af73   Al Viro   consolidate namei...
1105
1106
  		if (unlazy_walk(nd, dentry))
  			return -ECHILD;
5a18fff20   Al Viro   untangle do_lookup()
1107
1108
  	} else {
  		dentry = __d_lookup(parent, name);
9875cf806   David Howells   Add a dentry op t...
1109
  	}
5a18fff20   Al Viro   untangle do_lookup()
1110

44396f4b5   Josef Bacik   fs: add a DCACHE_...
1111
1112
1113
1114
  	if (dentry && unlikely(d_need_lookup(dentry))) {
  		dput(dentry);
  		dentry = NULL;
  	}
5a18fff20   Al Viro   untangle do_lookup()
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
  retry:
  	if (unlikely(!dentry)) {
  		struct inode *dir = parent->d_inode;
  		BUG_ON(nd->inode != dir);
  
  		mutex_lock(&dir->i_mutex);
  		dentry = d_lookup(parent, name);
  		if (likely(!dentry)) {
  			dentry = d_alloc_and_lookup(parent, name, nd);
  			if (IS_ERR(dentry)) {
  				mutex_unlock(&dir->i_mutex);
  				return PTR_ERR(dentry);
  			}
  			/* known good */
  			need_reval = 0;
  			status = 1;
44396f4b5   Josef Bacik   fs: add a DCACHE_...
1131
1132
1133
1134
1135
1136
1137
1138
1139
  		} else if (unlikely(d_need_lookup(dentry))) {
  			dentry = d_inode_lookup(parent, dentry, nd);
  			if (IS_ERR(dentry)) {
  				mutex_unlock(&dir->i_mutex);
  				return PTR_ERR(dentry);
  			}
  			/* known good */
  			need_reval = 0;
  			status = 1;
5a18fff20   Al Viro   untangle do_lookup()
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
  		}
  		mutex_unlock(&dir->i_mutex);
  	}
  	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  		status = d_revalidate(dentry, nd);
  	if (unlikely(status <= 0)) {
  		if (status < 0) {
  			dput(dentry);
  			return status;
  		}
  		if (!d_invalidate(dentry)) {
  			dput(dentry);
  			dentry = NULL;
  			need_reval = 1;
  			goto retry;
  		}
24643087e   Al Viro   in do_lookup() sp...
1156
  	}
5a18fff20   Al Viro   untangle do_lookup()
1157

9875cf806   David Howells   Add a dentry op t...
1158
1159
1160
  	path->mnt = mnt;
  	path->dentry = dentry;
  	err = follow_managed(path, nd->flags);
893122141   Ian Kent   vfs - fix dentry ...
1161
1162
  	if (unlikely(err < 0)) {
  		path_put_conditional(path, nd);
9875cf806   David Howells   Add a dentry op t...
1163
  		return err;
893122141   Ian Kent   vfs - fix dentry ...
1164
  	}
a3fbbde70   Al Viro   VFS: we need to s...
1165
1166
  	if (err)
  		nd->flags |= LOOKUP_JUMPED;
9875cf806   David Howells   Add a dentry op t...
1167
  	*inode = path->dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1168
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1169
  }
52094c8a0   Al Viro   take RCU-dependen...
1170
1171
1172
  static inline int may_lookup(struct nameidata *nd)
  {
  	if (nd->flags & LOOKUP_RCU) {
4ad5abb3d   Al Viro   no reason to keep...
1173
  		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a0   Al Viro   take RCU-dependen...
1174
1175
  		if (err != -ECHILD)
  			return err;
19660af73   Al Viro   consolidate namei...
1176
  		if (unlazy_walk(nd, NULL))
52094c8a0   Al Viro   take RCU-dependen...
1177
1178
  			return -ECHILD;
  	}
4ad5abb3d   Al Viro   no reason to keep...
1179
  	return inode_permission(nd->inode, MAY_EXEC);
52094c8a0   Al Viro   take RCU-dependen...
1180
  }
9856fa1b2   Al Viro   pull handling of ...
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
  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...
1192
1193
1194
1195
1196
1197
  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...
1198
1199
  		if (!(nd->flags & LOOKUP_ROOT))
  			nd->root.mnt = NULL;
951361f95   Al Viro   get rid of the la...
1200
1201
1202
1203
  		rcu_read_unlock();
  		br_read_unlock(vfsmount_lock);
  	}
  }
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1204
1205
1206
1207
1208
1209
  /*
   * 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...
1210
  static inline int should_follow_link(struct inode *inode, int follow)
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
  {
  	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 ...
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
  static inline int walk_component(struct nameidata *nd, struct path *path,
  		struct qstr *name, int type, int follow)
  {
  	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.
  	 */
  	if (unlikely(type != LAST_NORM))
  		return handle_dots(nd, type);
  	err = do_lookup(nd, name, path, &inode);
  	if (unlikely(err)) {
  		terminate_walk(nd);
  		return err;
  	}
  	if (!inode) {
  		path_to_nameidata(path, nd);
  		terminate_walk(nd);
  		return -ENOENT;
  	}
7813b94a5   Linus Torvalds   vfs: rename 'do_f...
1245
  	if (should_follow_link(inode, follow)) {
19660af73   Al Viro   consolidate namei...
1246
1247
1248
1249
1250
1251
  		if (nd->flags & LOOKUP_RCU) {
  			if (unlikely(unlazy_walk(nd, path->dentry))) {
  				terminate_walk(nd);
  				return -ECHILD;
  			}
  		}
ce57dfc17   Al Viro   pull handling of ...
1252
1253
1254
1255
1256
1257
1258
  		BUG_ON(inode != path->dentry->d_inode);
  		return 1;
  	}
  	path_to_nameidata(path, nd);
  	nd->inode = inode;
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1259
  /*
b356379a0   Al Viro   Turn resolution o...
1260
1261
1262
1263
1264
1265
1266
1267
1268
   * 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...
1269
1270
1271
1272
1273
  	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 ...
1274
  	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
b356379a0   Al Viro   Turn resolution o...
1275
1276
1277
1278
1279
1280
1281
  
  	nd->depth++;
  	current->link_count++;
  
  	do {
  		struct path link = *path;
  		void *cookie;
574197e0d   Al Viro   tidy the trailing...
1282
1283
  
  		res = follow_link(&link, nd, &cookie);
b356379a0   Al Viro   Turn resolution o...
1284
1285
1286
  		if (!res)
  			res = walk_component(nd, path, &nd->last,
  					     nd->last_type, LOOKUP_FOLLOW);
574197e0d   Al Viro   tidy the trailing...
1287
  		put_link(nd, &link, cookie);
b356379a0   Al Viro   Turn resolution o...
1288
1289
1290
1291
1292
1293
1294
1295
  	} while (res > 0);
  
  	current->link_count--;
  	nd->depth--;
  	return res;
  }
  
  /*
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
   * 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;
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1316
   * Name resolution.
ea3834d9f   Prasanna Meda   namei: add audit_...
1317
1318
   * 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
1319
   *
ea3834d9f   Prasanna Meda   namei: add audit_...
1320
1321
   * 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
1322
   */
6de88d729   Al Viro   kill __link_path_...
1323
  static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1324
1325
  {
  	struct path next;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1326
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1327
1328
1329
1330
  	
  	while (*name=='/')
  		name++;
  	if (!*name)
086e183a6   Al Viro   pull dropping RCU...
1331
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1332

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1333
1334
1335
1336
1337
  	/* At this point we know we have a real path component. */
  	for(;;) {
  		unsigned long hash;
  		struct qstr this;
  		unsigned int c;
fe479a580   Al Viro   merge component t...
1338
  		int type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1339

52094c8a0   Al Viro   take RCU-dependen...
1340
  		err = may_lookup(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
   		if (err)
  			break;
  
  		this.name = name;
  		c = *(const unsigned char *)name;
  
  		hash = init_name_hash();
  		do {
  			name++;
  			hash = partial_name_hash(c, hash);
  			c = *(const unsigned char *)name;
  		} while (c && (c != '/'));
  		this.len = name - (const char *) this.name;
  		this.hash = end_name_hash(hash);
fe479a580   Al Viro   merge component t...
1355
1356
1357
  		type = LAST_NORM;
  		if (this.name[0] == '.') switch (this.len) {
  			case 2:
16c2cd717   Al Viro   untangle the "nee...
1358
  				if (this.name[1] == '.') {
fe479a580   Al Viro   merge component t...
1359
  					type = LAST_DOTDOT;
16c2cd717   Al Viro   untangle the "nee...
1360
1361
  					nd->flags |= LOOKUP_JUMPED;
  				}
fe479a580   Al Viro   merge component t...
1362
1363
1364
1365
  				break;
  			case 1:
  				type = LAST_DOT;
  		}
5a202bcd7   Al Viro   sanitize pathname...
1366
1367
  		if (likely(type == LAST_NORM)) {
  			struct dentry *parent = nd->path.dentry;
16c2cd717   Al Viro   untangle the "nee...
1368
  			nd->flags &= ~LOOKUP_JUMPED;
5a202bcd7   Al Viro   sanitize pathname...
1369
1370
1371
1372
1373
1374
1375
  			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...
1376

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1377
1378
1379
1380
1381
  		/* remove trailing slashes? */
  		if (!c)
  			goto last_component;
  		while (*++name == '/');
  		if (!*name)
b356379a0   Al Viro   Turn resolution o...
1382
  			goto last_component;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1383

ce57dfc17   Al Viro   pull handling of ...
1384
1385
1386
  		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  		if (err < 0)
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1387

ce57dfc17   Al Viro   pull handling of ...
1388
  		if (err) {
b356379a0   Al Viro   Turn resolution o...
1389
  			err = nested_symlink(&next, nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1390
  			if (err)
a7472baba   Al Viro   make nameidata_de...
1391
  				return err;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1392
  		}
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1393
1394
  		if (can_lookup(nd->inode))
  			continue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1395
  		err = -ENOTDIR; 
3ddcd0569   Linus Torvalds   vfs: optimize ino...
1396
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1397
  		/* here ends the main loop */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1398
  last_component:
b356379a0   Al Viro   Turn resolution o...
1399
1400
  		nd->last = this;
  		nd->last_type = type;
086e183a6   Al Viro   pull dropping RCU...
1401
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1402
  	}
951361f95   Al Viro   get rid of the la...
1403
  	terminate_walk(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1404
1405
  	return err;
  }
70e9b3571   Al Viro   get rid of nd->file
1406
1407
  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 ...
1408
1409
1410
1411
1412
1413
  {
  	int retval = 0;
  	int fput_needed;
  	struct file *file;
  
  	nd->last_type = LAST_ROOT; /* if there are only slashes... */
16c2cd717   Al Viro   untangle the "nee...
1414
  	nd->flags = flags | LOOKUP_JUMPED;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1415
  	nd->depth = 0;
5b6ca027d   Al Viro   reduce vfs_path_l...
1416
1417
  	if (flags & LOOKUP_ROOT) {
  		struct inode *inode = nd->root.dentry->d_inode;
73d049a40   Al Viro   open-style analog...
1418
1419
1420
1421
1422
1423
1424
  		if (*name) {
  			if (!inode->i_op->lookup)
  				return -ENOTDIR;
  			retval = inode_permission(inode, MAY_EXEC);
  			if (retval)
  				return retval;
  		}
5b6ca027d   Al Viro   reduce vfs_path_l...
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
  		nd->path = nd->root;
  		nd->inode = inode;
  		if (flags & LOOKUP_RCU) {
  			br_read_lock(vfsmount_lock);
  			rcu_read_lock();
  			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  		} else {
  			path_get(&nd->path);
  		}
  		return 0;
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1436
  	nd->root.mnt = NULL;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1437
1438
  
  	if (*name=='/') {
e41f7d4ee   Al Viro   merge path_init a...
1439
1440
1441
1442
1443
1444
1445
1446
1447
  		if (flags & LOOKUP_RCU) {
  			br_read_lock(vfsmount_lock);
  			rcu_read_lock();
  			set_root_rcu(nd);
  		} else {
  			set_root(nd);
  			path_get(&nd->root);
  		}
  		nd->path = nd->root;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1448
  	} else if (dfd == AT_FDCWD) {
e41f7d4ee   Al Viro   merge path_init a...
1449
1450
1451
  		if (flags & LOOKUP_RCU) {
  			struct fs_struct *fs = current->fs;
  			unsigned seq;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1452

e41f7d4ee   Al Viro   merge path_init a...
1453
1454
  			br_read_lock(vfsmount_lock);
  			rcu_read_lock();
c28cc3646   Nick Piggin   fs: fs_struct use...
1455

e41f7d4ee   Al Viro   merge path_init a...
1456
1457
1458
1459
1460
1461
1462
1463
  			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 ...
1464
1465
  	} else {
  		struct dentry *dentry;
1abf0c718   Al Viro   New kind of open ...
1466
  		file = fget_raw_light(dfd, &fput_needed);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1467
1468
1469
1470
1471
  		retval = -EBADF;
  		if (!file)
  			goto out_fail;
  
  		dentry = file->f_path.dentry;
f52e0c113   Al Viro   New AT_... flag: ...
1472
1473
1474
1475
  		if (*name) {
  			retval = -ENOTDIR;
  			if (!S_ISDIR(dentry->d_inode->i_mode))
  				goto fput_fail;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1476

4ad5abb3d   Al Viro   no reason to keep...
1477
  			retval = inode_permission(dentry->d_inode, MAY_EXEC);
f52e0c113   Al Viro   New AT_... flag: ...
1478
1479
1480
  			if (retval)
  				goto fput_fail;
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1481
1482
  
  		nd->path = file->f_path;
e41f7d4ee   Al Viro   merge path_init a...
1483
1484
  		if (flags & LOOKUP_RCU) {
  			if (fput_needed)
70e9b3571   Al Viro   get rid of nd->file
1485
  				*fp = file;
e41f7d4ee   Al Viro   merge path_init a...
1486
1487
1488
1489
1490
1491
1492
  			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  			br_read_lock(vfsmount_lock);
  			rcu_read_lock();
  		} else {
  			path_get(&file->f_path);
  			fput_light(file, fput_needed);
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1493
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1494

31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1495
  	nd->inode = nd->path.dentry->d_inode;
9b4a9b14a   Al Viro   Preparations to c...
1496
  	return 0;
2dfdd266b   Josef 'Jeff' Sipek   fs: use path_walk...
1497

9b4a9b14a   Al Viro   Preparations to c...
1498
1499
1500
1501
1502
  fput_fail:
  	fput_light(file, fput_needed);
  out_fail:
  	return retval;
  }
bd92d7fed   Al Viro   Make trailing sym...
1503
1504
1505
1506
1507
1508
1509
1510
1511
  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;
  	return walk_component(nd, path, &nd->last, nd->last_type,
  					nd->flags & LOOKUP_FOLLOW);
  }
9b4a9b14a   Al Viro   Preparations to c...
1512
  /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
ee0827cd6   Al Viro   sanitize path_wal...
1513
  static int path_lookupat(int dfd, const char *name,
9b4a9b14a   Al Viro   Preparations to c...
1514
1515
  				unsigned int flags, struct nameidata *nd)
  {
70e9b3571   Al Viro   get rid of nd->file
1516
  	struct file *base = NULL;
bd92d7fed   Al Viro   Make trailing sym...
1517
1518
  	struct path path;
  	int err;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
  
  	/*
  	 * 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...
1534
  	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
ee0827cd6   Al Viro   sanitize path_wal...
1535

bd92d7fed   Al Viro   Make trailing sym...
1536
1537
  	if (unlikely(err))
  		return err;
ee0827cd6   Al Viro   sanitize path_wal...
1538
1539
  
  	current->total_link_count = 0;
bd92d7fed   Al Viro   Make trailing sym...
1540
1541
1542
  	err = link_path_walk(name, nd);
  
  	if (!err && !(flags & LOOKUP_PARENT)) {
bd92d7fed   Al Viro   Make trailing sym...
1543
1544
1545
1546
  		err = lookup_last(nd, &path);
  		while (err > 0) {
  			void *cookie;
  			struct path link = path;
bd92d7fed   Al Viro   Make trailing sym...
1547
  			nd->flags |= LOOKUP_PARENT;
574197e0d   Al Viro   tidy the trailing...
1548
  			err = follow_link(&link, nd, &cookie);
bd92d7fed   Al Viro   Make trailing sym...
1549
1550
  			if (!err)
  				err = lookup_last(nd, &path);
574197e0d   Al Viro   tidy the trailing...
1551
  			put_link(nd, &link, cookie);
bd92d7fed   Al Viro   Make trailing sym...
1552
1553
  		}
  	}
ee0827cd6   Al Viro   sanitize path_wal...
1554

9f1fafee9   Al Viro   merge handle_reva...
1555
1556
  	if (!err)
  		err = complete_walk(nd);
bd92d7fed   Al Viro   Make trailing sym...
1557
1558
1559
1560
  
  	if (!err && nd->flags & LOOKUP_DIRECTORY) {
  		if (!nd->inode->i_op->lookup) {
  			path_put(&nd->path);
bd23a539d   Al Viro   fix leaks in path...
1561
  			err = -ENOTDIR;
bd92d7fed   Al Viro   Make trailing sym...
1562
1563
  		}
  	}
16c2cd717   Al Viro   untangle the "nee...
1564

70e9b3571   Al Viro   get rid of nd->file
1565
1566
  	if (base)
  		fput(base);
ee0827cd6   Al Viro   sanitize path_wal...
1567

5b6ca027d   Al Viro   reduce vfs_path_l...
1568
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2a7378711   Al Viro   Cache root in nam...
1569
1570
1571
  		path_put(&nd->root);
  		nd->root.mnt = NULL;
  	}
bd92d7fed   Al Viro   Make trailing sym...
1572
  	return err;
ee0827cd6   Al Viro   sanitize path_wal...
1573
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1574

ee0827cd6   Al Viro   sanitize path_wal...
1575
1576
1577
1578
1579
1580
1581
1582
  static int do_path_lookup(int dfd, const char *name,
  				unsigned int flags, struct nameidata *nd)
  {
  	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  	if (unlikely(retval == -ECHILD))
  		retval = path_lookupat(dfd, name, flags, nd);
  	if (unlikely(retval == -ESTALE))
  		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1583
1584
1585
1586
1587
1588
1589
  
  	if (likely(!retval)) {
  		if (unlikely(!audit_dummy_context())) {
  			if (nd->path.dentry && nd->inode)
  				audit_inode(name, nd->path.dentry);
  		}
  	}
170aa3d02   Ulrich Drepper   [PATCH] namei.c: ...
1590
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1591
  }
c9c6cac0c   Al Viro   kill path_lookup()
1592
  int kern_path_parent(const char *name, struct nameidata *nd)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
1593
  {
c9c6cac0c   Al Viro   kill path_lookup()
1594
  	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
1595
  }
d18114657   Al Viro   [PATCH] new helpe...
1596
1597
1598
1599
1600
1601
1602
1603
  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...
1604
1605
1606
1607
1608
1609
  /**
   * 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...
1610
   * @path: pointer to struct path to fill
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1611
1612
1613
   */
  int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  		    const char *name, unsigned int flags,
e0a012493   Al Viro   switch vfs_path_l...
1614
  		    struct path *path)
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1615
  {
e0a012493   Al Viro   switch vfs_path_l...
1616
1617
1618
1619
1620
  	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...
1621
  	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
e0a012493   Al Viro   switch vfs_path_l...
1622
1623
1624
1625
  	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...
1626
  }
eead19115   Christoph Hellwig   partially fix up ...
1627
1628
  static struct dentry *__lookup_hash(struct qstr *name,
  		struct dentry *base, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1629
  {
81fca4440   Christoph Hellwig   fs: move permissi...
1630
  	struct inode *inode = base->d_inode;
057f6c019   James Morris   security: prevent...
1631
  	struct dentry *dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1632
  	int err;
4ad5abb3d   Al Viro   no reason to keep...
1633
  	err = inode_permission(inode, MAY_EXEC);
81fca4440   Christoph Hellwig   fs: move permissi...
1634
1635
  	if (err)
  		return ERR_PTR(err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1636
1637
  
  	/*
b04f784e5   Nick Piggin   fs: remove extra ...
1638
1639
1640
  	 * Don't bother with __d_lookup: callers are for creat as
  	 * well as unlink, so a lot of the time it would cost
  	 * a double lookup.
6e6b1bd1e   Al Viro   Kill cached_looku...
1641
  	 */
b04f784e5   Nick Piggin   fs: remove extra ...
1642
  	dentry = d_lookup(base, name);
6e6b1bd1e   Al Viro   Kill cached_looku...
1643

44396f4b5   Josef Bacik   fs: add a DCACHE_...
1644
1645
1646
1647
1648
1649
1650
1651
1652
  	if (dentry && d_need_lookup(dentry)) {
  		/*
  		 * __lookup_hash is called with the parent dir's i_mutex already
  		 * held, so we are good to go here.
  		 */
  		dentry = d_inode_lookup(base, dentry, nd);
  		if (IS_ERR(dentry))
  			return dentry;
  	}
d2d9e9fbc   Al Viro   merge do_revalida...
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
  	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  		int status = d_revalidate(dentry, nd);
  		if (unlikely(status <= 0)) {
  			/*
  			 * The dentry failed validation.
  			 * If d_revalidate returned 0 attempt to invalidate
  			 * the dentry otherwise d_revalidate is asking us
  			 * to return a fail status.
  			 */
  			if (status < 0) {
  				dput(dentry);
  				return ERR_PTR(status);
  			} else if (!d_invalidate(dentry)) {
  				dput(dentry);
  				dentry = NULL;
  			}
  		}
  	}
6e6b1bd1e   Al Viro   Kill cached_looku...
1671

baa038907   Nick Piggin   fs: dentry alloca...
1672
1673
  	if (!dentry)
  		dentry = d_alloc_and_lookup(base, name, nd);
5a202bcd7   Al Viro   sanitize pathname...
1674

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1675
1676
  	return dentry;
  }
057f6c019   James Morris   security: prevent...
1677
1678
1679
1680
1681
  /*
   * 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 ...
1682
  static struct dentry *lookup_hash(struct nameidata *nd)
057f6c019   James Morris   security: prevent...
1683
  {
4ac913785   Jan Blunck   Embed a struct pa...
1684
  	return __lookup_hash(&nd->last, nd->path.dentry, nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1685
  }
eead19115   Christoph Hellwig   partially fix up ...
1686
  /**
a6b91919e   Randy Dunlap   fs: fix kernel-do...
1687
   * lookup_one_len - filesystem helper to lookup single pathname component
eead19115   Christoph Hellwig   partially fix up ...
1688
1689
1690
1691
   * @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...
1692
1693
   * 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 ...
1694
1695
1696
   * 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...
1697
1698
  struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  {
057f6c019   James Morris   security: prevent...
1699
  	struct qstr this;
6a96ba544   Al Viro   kill __lookup_one...
1700
1701
  	unsigned long hash;
  	unsigned int c;
057f6c019   James Morris   security: prevent...
1702

2f9092e10   David Woodhouse   Fix i_mutex vs. r...
1703
  	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
6a96ba544   Al Viro   kill __lookup_one...
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
  	this.name = name;
  	this.len = len;
  	if (!len)
  		return ERR_PTR(-EACCES);
  
  	hash = init_name_hash();
  	while (len--) {
  		c = *(const unsigned char *)name++;
  		if (c == '/' || c == '\0')
  			return ERR_PTR(-EACCES);
  		hash = partial_name_hash(c, hash);
  	}
  	this.hash = end_name_hash(hash);
5a202bcd7   Al Viro   sanitize pathname...
1717
1718
1719
1720
1721
1722
1723
1724
1725
  	/*
  	 * 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 ...
1726

49705b774   Christoph Hellwig   [PATCH] sanitize ...
1727
  	return __lookup_hash(&this, base, NULL);
057f6c019   James Morris   security: prevent...
1728
  }
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
1729
1730
  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
1731
  {
2d8f30380   Al Viro   [PATCH] sanitize ...
1732
  	struct nameidata nd;
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
1733
  	char *tmp = getname_flags(name, flags, empty);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1734
  	int err = PTR_ERR(tmp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1735
  	if (!IS_ERR(tmp)) {
2d8f30380   Al Viro   [PATCH] sanitize ...
1736
1737
1738
1739
  
  		BUG_ON(flags & LOOKUP_PARENT);
  
  		err = do_path_lookup(dfd, tmp, flags, &nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1740
  		putname(tmp);
2d8f30380   Al Viro   [PATCH] sanitize ...
1741
1742
  		if (!err)
  			*path = nd.path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1743
1744
1745
  	}
  	return err;
  }
1fa1e7f61   Andy Whitcroft   readlinkat: ensur...
1746
1747
1748
1749
1750
  int user_path_at(int dfd, const char __user *name, unsigned flags,
  		 struct path *path)
  {
  	return user_path_at_empty(dfd, name, flags, path, 0);
  }
2ad94ae65   Al Viro   [PATCH] new (loca...
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
  static int user_path_parent(int dfd, const char __user *path,
  			struct nameidata *nd, char **name)
  {
  	char *s = getname(path);
  	int error;
  
  	if (IS_ERR(s))
  		return PTR_ERR(s);
  
  	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  	if (error)
  		putname(s);
  	else
  		*name = s;
  
  	return error;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1768
1769
1770
1771
1772
1773
  /*
   * 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)
  {
da9592ede   David Howells   CRED: Wrap task c...
1774
  	uid_t fsuid = current_fsuid();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1775
1776
  	if (!(dir->i_mode & S_ISVTX))
  		return 0;
e795b7179   Serge E. Hallyn   userns: userns: c...
1777
1778
  	if (current_user_ns() != inode_userns(inode))
  		goto other_userns;
da9592ede   David Howells   CRED: Wrap task c...
1779
  	if (inode->i_uid == fsuid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1780
  		return 0;
da9592ede   David Howells   CRED: Wrap task c...
1781
  	if (dir->i_uid == fsuid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1782
  		return 0;
e795b7179   Serge E. Hallyn   userns: userns: c...
1783
1784
1785
  
  other_userns:
  	return !ns_capable(inode_userns(inode), CAP_FOWNER);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
  }
  
  /*
   *	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...
1807
  static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1808
1809
1810
1811
1812
1813
1814
  {
  	int error;
  
  	if (!victim->d_inode)
  		return -ENOENT;
  
  	BUG_ON(victim->d_parent->d_inode != dir);
cccc6bba3   Al Viro   Lose the first ar...
1815
  	audit_inode_child(victim, dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1816

f419a2e3b   Al Viro   [PATCH] kill name...
1817
  	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1818
1819
1820
1821
1822
  	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...
1823
  	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
  		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: ...
1847
  static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1848
1849
1850
1851
1852
  {
  	if (child->d_inode)
  		return -EEXIST;
  	if (IS_DEADDIR(dir))
  		return -ENOENT;
f419a2e3b   Al Viro   [PATCH] kill name...
1853
  	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1854
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1855
1856
1857
1858
1859
1860
1861
1862
  /*
   * 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: ...
1863
  		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1864
1865
  		return NULL;
  	}
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
1866
  	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1867

e2761a116   OGAWA Hirofumi   [PATCH vfs-2.6 2/...
1868
1869
1870
1871
1872
  	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
1873
  	}
e2761a116   OGAWA Hirofumi   [PATCH vfs-2.6 2/...
1874
1875
1876
1877
1878
  	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
1879
  	}
f2eace23e   Ingo Molnar   [PATCH] lockdep: ...
1880
1881
  	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
1882
1883
1884
1885
1886
  	return NULL;
  }
  
  void unlock_rename(struct dentry *p1, struct dentry *p2)
  {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
1887
  	mutex_unlock(&p1->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1888
  	if (p1 != p2) {
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
1889
  		mutex_unlock(&p2->d_inode->i_mutex);
a11f3a057   Arjan van de Ven   [PATCH] sem2mutex...
1890
  		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1891
1892
  	}
  }
4acdaf27e   Al Viro   switch ->create()...
1893
  int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1894
1895
  		struct nameidata *nd)
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
1896
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1897
1898
1899
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
1900
  	if (!dir->i_op->create)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1901
1902
1903
1904
1905
1906
  		return -EACCES;	/* shouldn't it be ENOSYS? */
  	mode &= S_IALLUGO;
  	mode |= S_IFREG;
  	error = security_inode_create(dir, dentry, mode);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1907
  	error = dir->i_op->create(dir, dentry, mode, nd);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
1908
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
1909
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1910
1911
  	return error;
  }
73d049a40   Al Viro   open-style analog...
1912
  static int may_open(struct path *path, int acc_mode, int flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1913
  {
3fb64190a   Christoph Hellwig   pass a struct pat...
1914
  	struct dentry *dentry = path->dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1915
1916
  	struct inode *inode = dentry->d_inode;
  	int error;
bcda76524   Al Viro   Allow O_PATH for ...
1917
1918
1919
  	/* O_PATH? */
  	if (!acc_mode)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1920
1921
  	if (!inode)
  		return -ENOENT;
c8fe8f30c   Christoph Hellwig   cleanup may_open
1922
1923
  	switch (inode->i_mode & S_IFMT) {
  	case S_IFLNK:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1924
  		return -ELOOP;
c8fe8f30c   Christoph Hellwig   cleanup may_open
1925
1926
1927
1928
1929
1930
  	case S_IFDIR:
  		if (acc_mode & MAY_WRITE)
  			return -EISDIR;
  		break;
  	case S_IFBLK:
  	case S_IFCHR:
3fb64190a   Christoph Hellwig   pass a struct pat...
1931
  		if (path->mnt->mnt_flags & MNT_NODEV)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1932
  			return -EACCES;
c8fe8f30c   Christoph Hellwig   cleanup may_open
1933
1934
1935
  		/*FALLTHRU*/
  	case S_IFIFO:
  	case S_IFSOCK:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1936
  		flag &= ~O_TRUNC;
c8fe8f30c   Christoph Hellwig   cleanup may_open
1937
  		break;
4a3fd211c   Dave Hansen   [PATCH] r/o bind ...
1938
  	}
b41572e92   Dave Hansen   r/o bind mounts: ...
1939

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1944
1945
1946
1947
  	/*
  	 * An append-only file must be opened in append mode for writing.
  	 */
  	if (IS_APPEND(inode)) {
8737c9305   Al Viro   Switch may_open()...
1948
  		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b5212   Al Viro   O_TRUNC open shou...
1949
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1950
  		if (flag & O_TRUNC)
7715b5212   Al Viro   O_TRUNC open shou...
1951
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1952
1953
1954
  	}
  
  	/* O_NOATIME can only be set by the owner or superuser */
2e1496707   Serge E. Hallyn   userns: rename is...
1955
  	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b5212   Al Viro   O_TRUNC open shou...
1956
  		return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1957

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

e1181ee65   Jeff Layton   vfs: pass struct ...
1961
  static int handle_truncate(struct file *filp)
7715b5212   Al Viro   O_TRUNC open shou...
1962
  {
e1181ee65   Jeff Layton   vfs: pass struct ...
1963
  	struct path *path = &filp->f_path;
7715b5212   Al Viro   O_TRUNC open shou...
1964
1965
1966
1967
1968
1969
1970
1971
1972
  	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...
1973
  		error = security_path_truncate(path);
7715b5212   Al Viro   O_TRUNC open shou...
1974
1975
1976
  	if (!error) {
  		error = do_truncate(path->dentry, 0,
  				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee65   Jeff Layton   vfs: pass struct ...
1977
  				    filp);
7715b5212   Al Viro   O_TRUNC open shou...
1978
1979
  	}
  	put_write_access(inode);
acd0c9351   Mimi Zohar   IMA: update ima_c...
1980
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1981
  }
d57999e15   Dave Hansen   [PATCH] do namei_...
1982
1983
  static inline int open_to_namei_flags(int flag)
  {
8a5e929dd   Al Viro   don't translitera...
1984
1985
  	if ((flag & O_ACCMODE) == 3)
  		flag--;
d57999e15   Dave Hansen   [PATCH] do namei_...
1986
1987
  	return flag;
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1988
  /*
fe2d35ff0   Al Viro   switch non-create...
1989
   * Handle the last step of open()
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1990
   */
fb1cc555d   Al Viro   gut do_filp_open(...
1991
  static struct file *do_last(struct nameidata *nd, struct path *path,
c3e380b0b   Al Viro   Collect "operatio...
1992
  			    const struct open_flags *op, const char *pathname)
fb1cc555d   Al Viro   gut do_filp_open(...
1993
  {
a1e28038d   Al Viro   pull the common p...
1994
  	struct dentry *dir = nd->path.dentry;
6c0d46c49   Al Viro   fold __open_namei...
1995
  	struct dentry *dentry;
ca344a894   Al Viro   do_last: unify ma...
1996
  	int open_flag = op->open_flag;
6c0d46c49   Al Viro   fold __open_namei...
1997
  	int will_truncate = open_flag & O_TRUNC;
ca344a894   Al Viro   do_last: unify ma...
1998
  	int want_write = 0;
bcda76524   Al Viro   Allow O_PATH for ...
1999
  	int acc_mode = op->acc_mode;
fb1cc555d   Al Viro   gut do_filp_open(...
2000
  	struct file *filp;
16c2cd717   Al Viro   untangle the "nee...
2001
  	int error;
1f36f774b   Al Viro   Switch !O_CREAT c...
2002

c3e380b0b   Al Viro   Collect "operatio...
2003
2004
  	nd->flags &= ~LOOKUP_PARENT;
  	nd->flags |= op->intent;
1f36f774b   Al Viro   Switch !O_CREAT c...
2005
2006
  	switch (nd->last_type) {
  	case LAST_DOTDOT:
176306f59   Neil Brown   VFS: fix recent b...
2007
  	case LAST_DOT:
fe2d35ff0   Al Viro   switch non-create...
2008
2009
2010
  		error = handle_dots(nd, nd->last_type);
  		if (error)
  			return ERR_PTR(error);
1f36f774b   Al Viro   Switch !O_CREAT c...
2011
  		/* fallthrough */
1f36f774b   Al Viro   Switch !O_CREAT c...
2012
  	case LAST_ROOT:
9f1fafee9   Al Viro   merge handle_reva...
2013
  		error = complete_walk(nd);
16c2cd717   Al Viro   untangle the "nee...
2014
  		if (error)
9f1fafee9   Al Viro   merge handle_reva...
2015
  			return ERR_PTR(error);
fe2d35ff0   Al Viro   switch non-create...
2016
  		audit_inode(pathname, nd->path.dentry);
ca344a894   Al Viro   do_last: unify ma...
2017
  		if (open_flag & O_CREAT) {
fe2d35ff0   Al Viro   switch non-create...
2018
2019
2020
2021
  			error = -EISDIR;
  			goto exit;
  		}
  		goto ok;
1f36f774b   Al Viro   Switch !O_CREAT c...
2022
  	case LAST_BIND:
9f1fafee9   Al Viro   merge handle_reva...
2023
  		error = complete_walk(nd);
16c2cd717   Al Viro   untangle the "nee...
2024
  		if (error)
9f1fafee9   Al Viro   merge handle_reva...
2025
  			return ERR_PTR(error);
1f36f774b   Al Viro   Switch !O_CREAT c...
2026
  		audit_inode(pathname, dir);
67ee3ad21   Al Viro   Pull handling of ...
2027
  		goto ok;
1f36f774b   Al Viro   Switch !O_CREAT c...
2028
  	}
67ee3ad21   Al Viro   Pull handling of ...
2029

ca344a894   Al Viro   do_last: unify ma...
2030
  	if (!(open_flag & O_CREAT)) {
bcda76524   Al Viro   Allow O_PATH for ...
2031
  		int symlink_ok = 0;
fe2d35ff0   Al Viro   switch non-create...
2032
2033
  		if (nd->last.name[nd->last.len])
  			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
bcda76524   Al Viro   Allow O_PATH for ...
2034
2035
  		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  			symlink_ok = 1;
fe2d35ff0   Al Viro   switch non-create...
2036
  		/* we _can_ be in RCU mode here */
ce57dfc17   Al Viro   pull handling of ...
2037
2038
2039
  		error = walk_component(nd, path, &nd->last, LAST_NORM,
  					!symlink_ok);
  		if (error < 0)
fe2d35ff0   Al Viro   switch non-create...
2040
  			return ERR_PTR(error);
ce57dfc17   Al Viro   pull handling of ...
2041
  		if (error) /* symlink */
fe2d35ff0   Al Viro   switch non-create...
2042
  			return NULL;
fe2d35ff0   Al Viro   switch non-create...
2043
  		/* sayonara */
9f1fafee9   Al Viro   merge handle_reva...
2044
2045
2046
  		error = complete_walk(nd);
  		if (error)
  			return ERR_PTR(-ECHILD);
fe2d35ff0   Al Viro   switch non-create...
2047
2048
2049
  
  		error = -ENOTDIR;
  		if (nd->flags & LOOKUP_DIRECTORY) {
ce57dfc17   Al Viro   pull handling of ...
2050
  			if (!nd->inode->i_op->lookup)
fe2d35ff0   Al Viro   switch non-create...
2051
2052
2053
2054
2055
2056
2057
  				goto exit;
  		}
  		audit_inode(pathname, nd->path.dentry);
  		goto ok;
  	}
  
  	/* create side of things */
a3fbbde70   Al Viro   VFS: we need to s...
2058
2059
2060
2061
  	/*
  	 * 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
  	 */
9f1fafee9   Al Viro   merge handle_reva...
2062
2063
2064
  	error = complete_walk(nd);
  	if (error)
  		return ERR_PTR(error);
fe2d35ff0   Al Viro   switch non-create...
2065
2066
  
  	audit_inode(pathname, dir);
16c2cd717   Al Viro   untangle the "nee...
2067
  	error = -EISDIR;
1f36f774b   Al Viro   Switch !O_CREAT c...
2068
  	/* trailing slashes? */
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2069
2070
  	if (nd->last.name[nd->last.len])
  		goto exit;
a2c36b450   Al Viro   pull more into do...
2071

a1e28038d   Al Viro   pull the common p...
2072
  	mutex_lock(&dir->d_inode->i_mutex);
6c0d46c49   Al Viro   fold __open_namei...
2073
2074
2075
  	dentry = lookup_hash(nd);
  	error = PTR_ERR(dentry);
  	if (IS_ERR(dentry)) {
fb1cc555d   Al Viro   gut do_filp_open(...
2076
2077
2078
  		mutex_unlock(&dir->d_inode->i_mutex);
  		goto exit;
  	}
6c0d46c49   Al Viro   fold __open_namei...
2079
2080
  	path->dentry = dentry;
  	path->mnt = nd->path.mnt;
fb1cc555d   Al Viro   gut do_filp_open(...
2081
  	/* Negative dentry, just create the file */
6c0d46c49   Al Viro   fold __open_namei...
2082
  	if (!dentry->d_inode) {
a218d0fdc   Al Viro   switch open and m...
2083
  		umode_t mode = op->mode;
6c0d46c49   Al Viro   fold __open_namei...
2084
2085
  		if (!IS_POSIXACL(dir->d_inode))
  			mode &= ~current_umask();
fb1cc555d   Al Viro   gut do_filp_open(...
2086
2087
  		/*
  		 * This write is needed to ensure that a
6c0d46c49   Al Viro   fold __open_namei...
2088
  		 * rw->ro transition does not occur between
fb1cc555d   Al Viro   gut do_filp_open(...
2089
2090
2091
2092
2093
2094
2095
  		 * the time when the file is created and when
  		 * a permanent write count is taken through
  		 * the 'struct file' in nameidata_to_filp().
  		 */
  		error = mnt_want_write(nd->path.mnt);
  		if (error)
  			goto exit_mutex_unlock;
ca344a894   Al Viro   do_last: unify ma...
2096
  		want_write = 1;
9b44f1b39   Al Viro   move may_open() f...
2097
  		/* Don't check for write permission, don't truncate */
ca344a894   Al Viro   do_last: unify ma...
2098
  		open_flag &= ~O_TRUNC;
6c0d46c49   Al Viro   fold __open_namei...
2099
  		will_truncate = 0;
bcda76524   Al Viro   Allow O_PATH for ...
2100
  		acc_mode = MAY_OPEN;
6c0d46c49   Al Viro   fold __open_namei...
2101
2102
2103
2104
2105
2106
2107
2108
2109
  		error = security_path_mknod(&nd->path, dentry, mode, 0);
  		if (error)
  			goto exit_mutex_unlock;
  		error = vfs_create(dir->d_inode, dentry, mode, nd);
  		if (error)
  			goto exit_mutex_unlock;
  		mutex_unlock(&dir->d_inode->i_mutex);
  		dput(nd->path.dentry);
  		nd->path.dentry = dentry;
ca344a894   Al Viro   do_last: unify ma...
2110
  		goto common;
fb1cc555d   Al Viro   gut do_filp_open(...
2111
2112
2113
2114
2115
2116
2117
2118
2119
  	}
  
  	/*
  	 * It already exists.
  	 */
  	mutex_unlock(&dir->d_inode->i_mutex);
  	audit_inode(pathname, path->dentry);
  
  	error = -EEXIST;
ca344a894   Al Viro   do_last: unify ma...
2120
  	if (open_flag & O_EXCL)
fb1cc555d   Al Viro   gut do_filp_open(...
2121
  		goto exit_dput;
9875cf806   David Howells   Add a dentry op t...
2122
2123
2124
  	error = follow_managed(path, nd->flags);
  	if (error < 0)
  		goto exit_dput;
fb1cc555d   Al Viro   gut do_filp_open(...
2125

a3fbbde70   Al Viro   VFS: we need to s...
2126
2127
  	if (error)
  		nd->flags |= LOOKUP_JUMPED;
fb1cc555d   Al Viro   gut do_filp_open(...
2128
2129
2130
  	error = -ENOENT;
  	if (!path->dentry->d_inode)
  		goto exit_dput;
9e67f3616   Al Viro   Kill is_link argu...
2131
2132
  
  	if (path->dentry->d_inode->i_op->follow_link)
fb1cc555d   Al Viro   gut do_filp_open(...
2133
  		return NULL;
fb1cc555d   Al Viro   gut do_filp_open(...
2134
2135
  
  	path_to_nameidata(path, nd);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2136
  	nd->inode = path->dentry->d_inode;
a3fbbde70   Al Viro   VFS: we need to s...
2137
2138
2139
2140
  	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
  	error = complete_walk(nd);
  	if (error)
  		goto exit;
fb1cc555d   Al Viro   gut do_filp_open(...
2141
  	error = -EISDIR;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2142
  	if (S_ISDIR(nd->inode->i_mode))
fb1cc555d   Al Viro   gut do_filp_open(...
2143
  		goto exit;
67ee3ad21   Al Viro   Pull handling of ...
2144
  ok:
6c0d46c49   Al Viro   fold __open_namei...
2145
2146
  	if (!S_ISREG(nd->inode->i_mode))
  		will_truncate = 0;
0f9d1a10c   Al Viro   expand finish_ope...
2147
2148
2149
2150
  	if (will_truncate) {
  		error = mnt_want_write(nd->path.mnt);
  		if (error)
  			goto exit;
ca344a894   Al Viro   do_last: unify ma...
2151
  		want_write = 1;
0f9d1a10c   Al Viro   expand finish_ope...
2152
  	}
ca344a894   Al Viro   do_last: unify ma...
2153
  common:
bcda76524   Al Viro   Allow O_PATH for ...
2154
  	error = may_open(&nd->path, acc_mode, open_flag);
ca344a894   Al Viro   do_last: unify ma...
2155
  	if (error)
0f9d1a10c   Al Viro   expand finish_ope...
2156
  		goto exit;
0f9d1a10c   Al Viro   expand finish_ope...
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
  	filp = nameidata_to_filp(nd);
  	if (!IS_ERR(filp)) {
  		error = ima_file_check(filp, op->acc_mode);
  		if (error) {
  			fput(filp);
  			filp = ERR_PTR(error);
  		}
  	}
  	if (!IS_ERR(filp)) {
  		if (will_truncate) {
  			error = handle_truncate(filp);
  			if (error) {
  				fput(filp);
  				filp = ERR_PTR(error);
  			}
  		}
  	}
ca344a894   Al Viro   do_last: unify ma...
2174
2175
  out:
  	if (want_write)
0f9d1a10c   Al Viro   expand finish_ope...
2176
2177
  		mnt_drop_write(nd->path.mnt);
  	path_put(&nd->path);
fb1cc555d   Al Viro   gut do_filp_open(...
2178
2179
2180
2181
2182
2183
2184
  	return filp;
  
  exit_mutex_unlock:
  	mutex_unlock(&dir->d_inode->i_mutex);
  exit_dput:
  	path_put_conditional(path, nd);
  exit:
ca344a894   Al Viro   do_last: unify ma...
2185
2186
  	filp = ERR_PTR(error);
  	goto out;
fb1cc555d   Al Viro   gut do_filp_open(...
2187
  }
13aab428a   Al Viro   separate -ESTALE/...
2188
  static struct file *path_openat(int dfd, const char *pathname,
73d049a40   Al Viro   open-style analog...
2189
  		struct nameidata *nd, const struct open_flags *op, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2190
  {
fe2d35ff0   Al Viro   switch non-create...
2191
  	struct file *base = NULL;
4a3fd211c   Dave Hansen   [PATCH] r/o bind ...
2192
  	struct file *filp;
9850c0565   Al Viro   Fix the -ESTALE h...
2193
  	struct path path;
13aab428a   Al Viro   separate -ESTALE/...
2194
  	int error;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2195
2196
2197
2198
  
  	filp = get_empty_filp();
  	if (!filp)
  		return ERR_PTR(-ENFILE);
47c805dc2   Al Viro   switch do_filp_op...
2199
  	filp->f_flags = op->open_flag;
73d049a40   Al Viro   open-style analog...
2200
2201
2202
  	nd->intent.open.file = filp;
  	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  	nd->intent.open.create_mode = op->mode;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2203

73d049a40   Al Viro   open-style analog...
2204
  	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2205
  	if (unlikely(error))
13aab428a   Al Viro   separate -ESTALE/...
2206
  		goto out_filp;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2207

fe2d35ff0   Al Viro   switch non-create...
2208
  	current->total_link_count = 0;
73d049a40   Al Viro   open-style analog...
2209
  	error = link_path_walk(pathname, nd);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2210
2211
  	if (unlikely(error))
  		goto out_filp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2212

73d049a40   Al Viro   open-style analog...
2213
  	filp = do_last(nd, &path, op, pathname);
806b681cb   Al Viro   Turn do_link spag...
2214
  	while (unlikely(!filp)) { /* trailing symlink */
7b9337aaf   Nick Piggin   fs: namei fix ->p...
2215
  		struct path link = path;
def4af30c   Al Viro   Get rid of symlin...
2216
  		void *cookie;
574197e0d   Al Viro   tidy the trailing...
2217
  		if (!(nd->flags & LOOKUP_FOLLOW)) {
73d049a40   Al Viro   open-style analog...
2218
2219
  			path_put_conditional(&path, nd);
  			path_put(&nd->path);
40b39136f   Al Viro   path_openat: clea...
2220
2221
2222
  			filp = ERR_PTR(-ELOOP);
  			break;
  		}
73d049a40   Al Viro   open-style analog...
2223
2224
  		nd->flags |= LOOKUP_PARENT;
  		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
574197e0d   Al Viro   tidy the trailing...
2225
  		error = follow_link(&link, nd, &cookie);
c3e380b0b   Al Viro   Collect "operatio...
2226
  		if (unlikely(error))
f1afe9efc   Al Viro   clean up the fail...
2227
  			filp = ERR_PTR(error);
c3e380b0b   Al Viro   Collect "operatio...
2228
  		else
73d049a40   Al Viro   open-style analog...
2229
  			filp = do_last(nd, &path, op, pathname);
574197e0d   Al Viro   tidy the trailing...
2230
  		put_link(nd, &link, cookie);
806b681cb   Al Viro   Turn do_link spag...
2231
  	}
10fa8e62f   Al Viro   Unify exits in O_...
2232
  out:
73d049a40   Al Viro   open-style analog...
2233
2234
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  		path_put(&nd->root);
fe2d35ff0   Al Viro   switch non-create...
2235
2236
  	if (base)
  		fput(base);
73d049a40   Al Viro   open-style analog...
2237
  	release_open_intent(nd);
10fa8e62f   Al Viro   Unify exits in O_...
2238
  	return filp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2239

31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2240
  out_filp:
806b681cb   Al Viro   Turn do_link spag...
2241
  	filp = ERR_PTR(error);
10fa8e62f   Al Viro   Unify exits in O_...
2242
  	goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2243
  }
13aab428a   Al Viro   separate -ESTALE/...
2244
2245
2246
  struct file *do_filp_open(int dfd, const char *pathname,
  		const struct open_flags *op, int flags)
  {
73d049a40   Al Viro   open-style analog...
2247
  	struct nameidata nd;
13aab428a   Al Viro   separate -ESTALE/...
2248
  	struct file *filp;
73d049a40   Al Viro   open-style analog...
2249
  	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
13aab428a   Al Viro   separate -ESTALE/...
2250
  	if (unlikely(filp == ERR_PTR(-ECHILD)))
73d049a40   Al Viro   open-style analog...
2251
  		filp = path_openat(dfd, pathname, &nd, op, flags);
13aab428a   Al Viro   separate -ESTALE/...
2252
  	if (unlikely(filp == ERR_PTR(-ESTALE)))
73d049a40   Al Viro   open-style analog...
2253
  		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
13aab428a   Al Viro   separate -ESTALE/...
2254
2255
  	return filp;
  }
73d049a40   Al Viro   open-style analog...
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
  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;
  
  	nd.root.mnt = mnt;
  	nd.root.dentry = dentry;
  
  	flags |= LOOKUP_ROOT;
bcda76524   Al Viro   Allow O_PATH for ...
2266
  	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
73d049a40   Al Viro   open-style analog...
2267
2268
2269
2270
2271
2272
2273
2274
2275
  		return ERR_PTR(-ELOOP);
  
  	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  	if (unlikely(file == ERR_PTR(-ECHILD)))
  		file = path_openat(-1, name, &nd, op, flags);
  	if (unlikely(file == ERR_PTR(-ESTALE)))
  		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  	return file;
  }
ed75e95de   Al Viro   kill lookup_create()
2276
  struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2277
  {
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2278
  	struct dentry *dentry = ERR_PTR(-EEXIST);
ed75e95de   Al Viro   kill lookup_create()
2279
2280
2281
2282
  	struct nameidata nd;
  	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  	if (error)
  		return ERR_PTR(error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2283

c663e5d80   Christoph Hellwig   [PATCH] add some ...
2284
2285
2286
2287
  	/*
  	 * Yucky last component or no last component at all?
  	 * (foo/., foo/.., /////)
  	 */
ed75e95de   Al Viro   kill lookup_create()
2288
2289
2290
2291
2292
  	if (nd.last_type != LAST_NORM)
  		goto out;
  	nd.flags &= ~LOOKUP_PARENT;
  	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  	nd.intent.open.flags = O_EXCL;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2293
2294
2295
2296
  
  	/*
  	 * Do the final lookup.
  	 */
ed75e95de   Al Viro   kill lookup_create()
2297
2298
  	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
2299
2300
  	if (IS_ERR(dentry))
  		goto fail;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2301

e9baf6e59   Al Viro   [PATCH] return to...
2302
2303
  	if (dentry->d_inode)
  		goto eexist;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2304
2305
2306
2307
2308
2309
  	/*
  	 * 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()
2310
  	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
e9baf6e59   Al Viro   [PATCH] return to...
2311
2312
  		dput(dentry);
  		dentry = ERR_PTR(-ENOENT);
ed75e95de   Al Viro   kill lookup_create()
2313
  		goto fail;
e9baf6e59   Al Viro   [PATCH] return to...
2314
  	}
ed75e95de   Al Viro   kill lookup_create()
2315
  	*path = nd.path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2316
  	return dentry;
e9baf6e59   Al Viro   [PATCH] return to...
2317
  eexist:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2318
  	dput(dentry);
e9baf6e59   Al Viro   [PATCH] return to...
2319
  	dentry = ERR_PTR(-EEXIST);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2320
  fail:
ed75e95de   Al Viro   kill lookup_create()
2321
2322
2323
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  out:
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2324
2325
  	return dentry;
  }
dae6ad8f3   Al Viro   new helpers: kern...
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
  EXPORT_SYMBOL(kern_path_create);
  
  struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
  {
  	char *tmp = getname(pathname);
  	struct dentry *res;
  	if (IS_ERR(tmp))
  		return ERR_CAST(tmp);
  	res = kern_path_create(dfd, tmp, path, is_dir);
  	putname(tmp);
  	return res;
  }
  EXPORT_SYMBOL(user_path_create);
1a67aafb5   Al Viro   switch ->mknod() ...
2339
  int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2340
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2341
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2342
2343
2344
  
  	if (error)
  		return error;
e795b7179   Serge E. Hallyn   userns: userns: c...
2345
2346
  	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  	    !ns_capable(inode_userns(dir), CAP_MKNOD))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2347
  		return -EPERM;
acfa4380e   Al Viro   inode->i_op is ne...
2348
  	if (!dir->i_op->mknod)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2349
  		return -EPERM;
08ce5f16e   Serge E. Hallyn   cgroups: implemen...
2350
2351
2352
  	error = devcgroup_inode_mknod(mode, dev);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2353
2354
2355
  	error = security_inode_mknod(dir, dentry, mode, dev);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2356
  	error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
2357
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
2358
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2359
2360
  	return error;
  }
f69aac000   Al Viro   switch may_mknod(...
2361
  static int may_mknod(umode_t mode)
463c31972   Dave Hansen   [PATCH] r/o bind ...
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
  {
  	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...
2377
  SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
2378
  		unsigned, dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2379
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
2380
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
2381
2382
  	struct path path;
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2383
2384
2385
  
  	if (S_ISDIR(mode))
  		return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2386

dae6ad8f3   Al Viro   new helpers: kern...
2387
2388
2389
  	dentry = user_path_create(dfd, filename, &path, 0);
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
2ad94ae65   Al Viro   [PATCH] new (loca...
2390

dae6ad8f3   Al Viro   new helpers: kern...
2391
  	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5   Al Viro   New helper - curr...
2392
  		mode &= ~current_umask();
463c31972   Dave Hansen   [PATCH] r/o bind ...
2393
2394
2395
  	error = may_mknod(mode);
  	if (error)
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
2396
  	error = mnt_want_write(path.mnt);
463c31972   Dave Hansen   [PATCH] r/o bind ...
2397
2398
  	if (error)
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
2399
  	error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2400
2401
  	if (error)
  		goto out_drop_write;
463c31972   Dave Hansen   [PATCH] r/o bind ...
2402
  	switch (mode & S_IFMT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2403
  		case 0: case S_IFREG:
dae6ad8f3   Al Viro   new helpers: kern...
2404
  			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2405
2406
  			break;
  		case S_IFCHR: case S_IFBLK:
dae6ad8f3   Al Viro   new helpers: kern...
2407
  			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2408
2409
2410
  					new_decode_dev(dev));
  			break;
  		case S_IFIFO: case S_IFSOCK:
dae6ad8f3   Al Viro   new helpers: kern...
2411
  			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2412
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2413
  	}
be6d3e56a   Kentaro Takeda   introduce new LSM...
2414
  out_drop_write:
dae6ad8f3   Al Viro   new helpers: kern...
2415
  	mnt_drop_write(path.mnt);
463c31972   Dave Hansen   [PATCH] r/o bind ...
2416
2417
  out_dput:
  	dput(dentry);
dae6ad8f3   Al Viro   new helpers: kern...
2418
2419
  	mutex_unlock(&path.dentry->d_inode->i_mutex);
  	path_put(&path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2420
2421
2422
  
  	return error;
  }
8208a22bb   Al Viro   switch sys_mknoda...
2423
  SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2424
2425
2426
  {
  	return sys_mknodat(AT_FDCWD, filename, mode, dev);
  }
18bb1db3e   Al Viro   switch vfs_mkdir(...
2427
  int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2428
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2429
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2430
2431
2432
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
2433
  	if (!dir->i_op->mkdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2434
2435
2436
2437
2438
2439
  		return -EPERM;
  
  	mode &= (S_IRWXUGO|S_ISVTX);
  	error = security_inode_mkdir(dir, dentry, mode);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2440
  	error = dir->i_op->mkdir(dir, dentry, mode);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
2441
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
2442
  		fsnotify_mkdir(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2443
2444
  	return error;
  }
a218d0fdc   Al Viro   switch open and m...
2445
  SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2446
  {
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2447
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
2448
2449
  	struct path path;
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2450

dae6ad8f3   Al Viro   new helpers: kern...
2451
  	dentry = user_path_create(dfd, pathname, &path, 1);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2452
  	if (IS_ERR(dentry))
dae6ad8f3   Al Viro   new helpers: kern...
2453
  		return PTR_ERR(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2454

dae6ad8f3   Al Viro   new helpers: kern...
2455
  	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5   Al Viro   New helper - curr...
2456
  		mode &= ~current_umask();
dae6ad8f3   Al Viro   new helpers: kern...
2457
  	error = mnt_want_write(path.mnt);
463c31972   Dave Hansen   [PATCH] r/o bind ...
2458
2459
  	if (error)
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
2460
  	error = security_path_mkdir(&path, dentry, mode);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2461
2462
  	if (error)
  		goto out_drop_write;
dae6ad8f3   Al Viro   new helpers: kern...
2463
  	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2464
  out_drop_write:
dae6ad8f3   Al Viro   new helpers: kern...
2465
  	mnt_drop_write(path.mnt);
463c31972   Dave Hansen   [PATCH] r/o bind ...
2466
  out_dput:
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2467
  	dput(dentry);
dae6ad8f3   Al Viro   new helpers: kern...
2468
2469
  	mutex_unlock(&path.dentry->d_inode->i_mutex);
  	path_put(&path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2470
2471
  	return error;
  }
a218d0fdc   Al Viro   switch open and m...
2472
  SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2473
2474
2475
  {
  	return sys_mkdirat(AT_FDCWD, pathname, mode);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2476
  /*
a71905f0d   Sage Weil   vfs: update dentr...
2477
2478
2479
2480
   * The dentry_unhash() helper will try to drop the dentry early: we
   * should have a usage count of 2 if we're the only user of this
   * 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
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
   *
   * 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...
2493
  	shrink_dcache_parent(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2494
  	spin_lock(&dentry->d_lock);
64252c75a   Sage Weil   vfs: remove dget(...
2495
  	if (dentry->d_count == 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2496
2497
  		__d_drop(dentry);
  	spin_unlock(&dentry->d_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2498
2499
2500
2501
2502
2503
2504
2505
  }
  
  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...
2506
  	if (!dir->i_op->rmdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2507
  		return -EPERM;
1d2ef5901   Al Viro   restore pinning t...
2508
  	dget(dentry);
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2509
  	mutex_lock(&dentry->d_inode->i_mutex);
912dbc15d   Sage Weil   vfs: clean up vfs...
2510
2511
  
  	error = -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2512
  	if (d_mountpoint(dentry))
912dbc15d   Sage Weil   vfs: clean up vfs...
2513
2514
2515
2516
2517
  		goto out;
  
  	error = security_inode_rmdir(dir, dentry);
  	if (error)
  		goto out;
3cebde241   Sage Weil   vfs: shrink_dcach...
2518
  	shrink_dcache_parent(dentry);
912dbc15d   Sage Weil   vfs: clean up vfs...
2519
2520
2521
2522
2523
2524
2525
2526
  	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...
2527
  	mutex_unlock(&dentry->d_inode->i_mutex);
1d2ef5901   Al Viro   restore pinning t...
2528
  	dput(dentry);
912dbc15d   Sage Weil   vfs: clean up vfs...
2529
  	if (!error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2530
  		d_delete(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2531
2532
  	return error;
  }
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2533
  static long do_rmdir(int dfd, const char __user *pathname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2534
2535
2536
2537
2538
  {
  	int error = 0;
  	char * name;
  	struct dentry *dentry;
  	struct nameidata nd;
2ad94ae65   Al Viro   [PATCH] new (loca...
2539
  	error = user_path_parent(dfd, pathname, &nd, &name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2540
  	if (error)
2ad94ae65   Al Viro   [PATCH] new (loca...
2541
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2542
2543
  
  	switch(nd.last_type) {
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
2544
2545
2546
2547
2548
2549
2550
2551
2552
  	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
2553
  	}
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
2554
2555
  
  	nd.flags &= ~LOOKUP_PARENT;
4ac913785   Jan Blunck   Embed a struct pa...
2556
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
2557
  	dentry = lookup_hash(&nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2558
  	error = PTR_ERR(dentry);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2559
2560
  	if (IS_ERR(dentry))
  		goto exit2;
e6bc45d65   Theodore Ts'o   vfs: make unlink(...
2561
2562
2563
2564
  	if (!dentry->d_inode) {
  		error = -ENOENT;
  		goto exit3;
  	}
0622753b8   Dave Hansen   [PATCH] r/o bind ...
2565
2566
2567
  	error = mnt_want_write(nd.path.mnt);
  	if (error)
  		goto exit3;
be6d3e56a   Kentaro Takeda   introduce new LSM...
2568
2569
2570
  	error = security_path_rmdir(&nd.path, dentry);
  	if (error)
  		goto exit4;
4ac913785   Jan Blunck   Embed a struct pa...
2571
  	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2572
  exit4:
0622753b8   Dave Hansen   [PATCH] r/o bind ...
2573
2574
  	mnt_drop_write(nd.path.mnt);
  exit3:
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2575
2576
  	dput(dentry);
  exit2:
4ac913785   Jan Blunck   Embed a struct pa...
2577
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2578
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
2579
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2580
2581
2582
  	putname(name);
  	return error;
  }
3cdad4288   Heiko Carstens   [CVE-2009-0029] S...
2583
  SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2584
2585
2586
  {
  	return do_rmdir(AT_FDCWD, pathname);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2587
2588
2589
2590
2591
2592
  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...
2593
  	if (!dir->i_op->unlink)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2594
  		return -EPERM;
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2595
  	mutex_lock(&dentry->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2596
2597
2598
2599
  	if (d_mountpoint(dentry))
  		error = -EBUSY;
  	else {
  		error = security_inode_unlink(dir, dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
2600
  		if (!error) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2601
  			error = dir->i_op->unlink(dir, dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
2602
  			if (!error)
d83c49f3e   Al Viro   Fix the regressio...
2603
  				dont_mount(dentry);
bec1052e5   Al Viro   set S_DEAD on unl...
2604
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2605
  	}
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2606
  	mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2607
2608
2609
  
  	/* 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_...
2610
  		fsnotify_link_count(dentry->d_inode);
e234f35c5   John McCutchan   [PATCH] inotify d...
2611
  		d_delete(dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2612
  	}
0eeca2830   Robert Love   [PATCH] inotify
2613

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2614
2615
2616
2617
2618
  	return error;
  }
  
  /*
   * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2619
   * directory's i_mutex.  Truncate can take a long time if there is a lot of
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2620
2621
2622
   * 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 ...
2623
  static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2624
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
2625
2626
  	int error;
  	char *name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2627
2628
2629
  	struct dentry *dentry;
  	struct nameidata nd;
  	struct inode *inode = NULL;
2ad94ae65   Al Viro   [PATCH] new (loca...
2630
  	error = user_path_parent(dfd, pathname, &nd, &name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2631
  	if (error)
2ad94ae65   Al Viro   [PATCH] new (loca...
2632
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2633
2634
2635
  	error = -EISDIR;
  	if (nd.last_type != LAST_NORM)
  		goto exit1;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
2636
2637
  
  	nd.flags &= ~LOOKUP_PARENT;
4ac913785   Jan Blunck   Embed a struct pa...
2638
  	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
2639
  	dentry = lookup_hash(&nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2640
2641
2642
  	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...
2643
2644
  		if (nd.last.name[nd.last.len])
  			goto slashes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2645
  		inode = dentry->d_inode;
50338b889   Török Edwin   fix wrong iput on...
2646
  		if (!inode)
e6bc45d65   Theodore Ts'o   vfs: make unlink(...
2647
2648
  			goto slashes;
  		ihold(inode);
0622753b8   Dave Hansen   [PATCH] r/o bind ...
2649
2650
2651
  		error = mnt_want_write(nd.path.mnt);
  		if (error)
  			goto exit2;
be6d3e56a   Kentaro Takeda   introduce new LSM...
2652
2653
2654
  		error = security_path_unlink(&nd.path, dentry);
  		if (error)
  			goto exit3;
4ac913785   Jan Blunck   Embed a struct pa...
2655
  		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2656
  exit3:
0622753b8   Dave Hansen   [PATCH] r/o bind ...
2657
  		mnt_drop_write(nd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2658
2659
2660
  	exit2:
  		dput(dentry);
  	}
4ac913785   Jan Blunck   Embed a struct pa...
2661
  	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2662
2663
2664
  	if (inode)
  		iput(inode);	/* truncate the inode here */
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
2665
  	path_put(&nd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2666
2667
2668
2669
2670
2671
2672
2673
  	putname(name);
  	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...
2674
  SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2675
2676
2677
2678
2679
2680
2681
2682
2683
  {
  	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...
2684
  SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2685
2686
2687
  {
  	return do_unlinkat(AT_FDCWD, pathname);
  }
db2e747b1   Miklos Szeredi   [patch 5/5] vfs: ...
2688
  int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2689
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2690
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2691
2692
2693
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
2694
  	if (!dir->i_op->symlink)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2695
2696
2697
2698
2699
  		return -EPERM;
  
  	error = security_inode_symlink(dir, dentry, oldname);
  	if (error)
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2700
  	error = dir->i_op->symlink(dir, dentry, oldname);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
2701
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
2702
  		fsnotify_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2703
2704
  	return error;
  }
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
2705
2706
  SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  		int, newdfd, const char __user *, newname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2707
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
2708
2709
  	int error;
  	char *from;
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2710
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
2711
  	struct path path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2712
2713
  
  	from = getname(oldname);
2ad94ae65   Al Viro   [PATCH] new (loca...
2714
  	if (IS_ERR(from))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2715
  		return PTR_ERR(from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2716

dae6ad8f3   Al Viro   new helpers: kern...
2717
  	dentry = user_path_create(newdfd, newname, &path, 0);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2718
2719
  	error = PTR_ERR(dentry);
  	if (IS_ERR(dentry))
dae6ad8f3   Al Viro   new helpers: kern...
2720
  		goto out_putname;
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2721

dae6ad8f3   Al Viro   new helpers: kern...
2722
  	error = mnt_want_write(path.mnt);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
2723
2724
  	if (error)
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
2725
  	error = security_path_symlink(&path, dentry, from);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2726
2727
  	if (error)
  		goto out_drop_write;
dae6ad8f3   Al Viro   new helpers: kern...
2728
  	error = vfs_symlink(path.dentry->d_inode, dentry, from);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2729
  out_drop_write:
dae6ad8f3   Al Viro   new helpers: kern...
2730
  	mnt_drop_write(path.mnt);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
2731
  out_dput:
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2732
  	dput(dentry);
dae6ad8f3   Al Viro   new helpers: kern...
2733
2734
  	mutex_unlock(&path.dentry->d_inode->i_mutex);
  	path_put(&path);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2735
  out_putname:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2736
2737
2738
  	putname(from);
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
2739
  SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2740
2741
2742
  {
  	return sys_symlinkat(oldname, AT_FDCWD, newname);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2743
2744
2745
2746
2747
2748
2749
  int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  {
  	struct inode *inode = old_dentry->d_inode;
  	int error;
  
  	if (!inode)
  		return -ENOENT;
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2750
  	error = may_create(dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
  	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...
2762
  	if (!dir->i_op->link)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2763
  		return -EPERM;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
2764
  	if (S_ISDIR(inode->i_mode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2765
2766
2767
2768
2769
  		return -EPERM;
  
  	error = security_inode_link(old_dentry, dir, new_dentry);
  	if (error)
  		return error;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
2770
  	mutex_lock(&inode->i_mutex);
aae8a97d3   Aneesh Kumar K.V   fs: Don't allow t...
2771
2772
2773
2774
2775
  	/* Make sure we don't allow creating hardlink to an unlinked file */
  	if (inode->i_nlink == 0)
  		error =  -ENOENT;
  	else
  		error = dir->i_op->link(old_dentry, dir, new_dentry);
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
2776
  	mutex_unlock(&inode->i_mutex);
e31e14ec3   Stephen Smalley   [PATCH] remove th...
2777
  	if (!error)
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
2778
  		fsnotify_link(dir, inode, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
  	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...
2791
2792
  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
2793
2794
  {
  	struct dentry *new_dentry;
dae6ad8f3   Al Viro   new helpers: kern...
2795
  	struct path old_path, new_path;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
2796
  	int how = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2797
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2798

11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
2799
  	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e16   Ulrich Drepper   [PATCH] flags par...
2800
  		return -EINVAL;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
  	/*
  	 * 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;
c04030e16   Ulrich Drepper   [PATCH] flags par...
2814

11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
2815
  	error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2816
  	if (error)
2ad94ae65   Al Viro   [PATCH] new (loca...
2817
  		return error;
dae6ad8f3   Al Viro   new helpers: kern...
2818
  	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2819
  	error = PTR_ERR(new_dentry);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2820
  	if (IS_ERR(new_dentry))
dae6ad8f3   Al Viro   new helpers: kern...
2821
2822
2823
2824
2825
2826
  		goto out;
  
  	error = -EXDEV;
  	if (old_path.mnt != new_path.mnt)
  		goto out_dput;
  	error = mnt_want_write(new_path.mnt);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
2827
2828
  	if (error)
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
2829
  	error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2830
2831
  	if (error)
  		goto out_drop_write;
dae6ad8f3   Al Viro   new helpers: kern...
2832
  	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2833
  out_drop_write:
dae6ad8f3   Al Viro   new helpers: kern...
2834
  	mnt_drop_write(new_path.mnt);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
2835
  out_dput:
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2836
  	dput(new_dentry);
dae6ad8f3   Al Viro   new helpers: kern...
2837
2838
  	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
  	path_put(&new_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2839
  out:
2d8f30380   Al Viro   [PATCH] sanitize ...
2840
  	path_put(&old_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2841
2842
2843
  
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
2844
  SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2845
  {
c04030e16   Ulrich Drepper   [PATCH] flags par...
2846
  	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2847
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2848
2849
2850
2851
2852
2853
2854
  /*
   * 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...
2855
   *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2856
2857
   *	   story.
   *	c) we have to lock _three_ objects - parents and victim (if it exists).
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2858
   *	   And that - after we got ->i_mutex on parents (until then we don't know
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2859
2860
   *	   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...
2861
   *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2862
2863
2864
   *	   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...
2865
   *	   lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2866
2867
2868
   *	   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_...
2869
   *	d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2870
   *	   we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2871
   *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e72   Adam Buchbinder   Fix misspellings ...
2872
   *	   ->i_mutex on parents, which works but leads to some truly excessive
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2873
2874
   *	   locking].
   */
75c96f858   Adrian Bunk   [PATCH] make some...
2875
2876
  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
2877
2878
  {
  	int error = 0;
9055cba71   Sage Weil   vfs: clean up vfs...
2879
  	struct inode *target = new_dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2880
2881
2882
2883
2884
2885
  
  	/*
  	 * 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...
2886
  		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2887
2888
2889
2890
2891
2892
2893
  		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...
2894
  	dget(new_dentry);
d83c49f3e   Al Viro   Fix the regressio...
2895
  	if (target)
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2896
  		mutex_lock(&target->i_mutex);
9055cba71   Sage Weil   vfs: clean up vfs...
2897
2898
2899
2900
  
  	error = -EBUSY;
  	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  		goto out;
3cebde241   Sage Weil   vfs: shrink_dcach...
2901
2902
  	if (target)
  		shrink_dcache_parent(new_dentry);
9055cba71   Sage Weil   vfs: clean up vfs...
2903
2904
2905
  	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
2906
  	if (target) {
9055cba71   Sage Weil   vfs: clean up vfs...
2907
2908
  		target->i_flags |= S_DEAD;
  		dont_mount(new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2909
  	}
9055cba71   Sage Weil   vfs: clean up vfs...
2910
2911
2912
  out:
  	if (target)
  		mutex_unlock(&target->i_mutex);
1d2ef5901   Al Viro   restore pinning t...
2913
  	dput(new_dentry);
e31e14ec3   Stephen Smalley   [PATCH] remove th...
2914
  	if (!error)
349457ccf   Mark Fasheh   [PATCH] Allow fil...
2915
2916
  		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
2917
2918
  	return error;
  }
75c96f858   Adrian Bunk   [PATCH] make some...
2919
2920
  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
2921
  {
51892bbb5   Sage Weil   vfs: clean up vfs...
2922
  	struct inode *target = new_dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2923
2924
2925
2926
2927
2928
2929
  	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
2930
  	if (target)
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2931
  		mutex_lock(&target->i_mutex);
51892bbb5   Sage Weil   vfs: clean up vfs...
2932
2933
  
  	error = -EBUSY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2934
  	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
51892bbb5   Sage Weil   vfs: clean up vfs...
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
  		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
2946
  	if (target)
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
2947
  		mutex_unlock(&target->i_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2948
2949
2950
2951
2952
2953
2954
2955
2956
  	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...
2957
  	const unsigned char *old_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2958
2959
2960
2961
2962
2963
2964
2965
2966
  
  	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: ...
2967
  		error = may_create(new_dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2968
2969
2970
2971
  	else
  		error = may_delete(new_dir, new_dentry, is_dir);
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
2972
  	if (!old_dir->i_op->rename)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2973
  		return -EPERM;
0eeca2830   Robert Love   [PATCH] inotify
2974
  	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2975
2976
2977
2978
  	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...
2979
2980
  	if (!error)
  		fsnotify_move(old_dir, new_dir, old_name, is_dir,
5a190ae69   Al Viro   [PATCH] pass dent...
2981
  			      new_dentry->d_inode, old_dentry);
0eeca2830   Robert Love   [PATCH] inotify
2982
  	fsnotify_oldname_free(old_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2983
2984
  	return error;
  }
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
2985
2986
  SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  		int, newdfd, const char __user *, newname)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2987
  {
2ad94ae65   Al Viro   [PATCH] new (loca...
2988
2989
2990
  	struct dentry *old_dir, *new_dir;
  	struct dentry *old_dentry, *new_dentry;
  	struct dentry *trap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2991
  	struct nameidata oldnd, newnd;
2ad94ae65   Al Viro   [PATCH] new (loca...
2992
2993
2994
  	char *from;
  	char *to;
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2995

2ad94ae65   Al Viro   [PATCH] new (loca...
2996
  	error = user_path_parent(olddfd, oldname, &oldnd, &from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2997
2998
  	if (error)
  		goto exit;
2ad94ae65   Al Viro   [PATCH] new (loca...
2999
  	error = user_path_parent(newdfd, newname, &newnd, &to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3000
3001
3002
3003
  	if (error)
  		goto exit1;
  
  	error = -EXDEV;
4ac913785   Jan Blunck   Embed a struct pa...
3004
  	if (oldnd.path.mnt != newnd.path.mnt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3005
  		goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
3006
  	old_dir = oldnd.path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3007
3008
3009
  	error = -EBUSY;
  	if (oldnd.last_type != LAST_NORM)
  		goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
3010
  	new_dir = newnd.path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3011
3012
  	if (newnd.last_type != LAST_NORM)
  		goto exit2;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3013
3014
  	oldnd.flags &= ~LOOKUP_PARENT;
  	newnd.flags &= ~LOOKUP_PARENT;
4e9ed2f85   OGAWA Hirofumi   [PATCH vfs-2.6 6/...
3015
  	newnd.flags |= LOOKUP_RENAME_TARGET;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
3016

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3017
  	trap = lock_rename(new_dir, old_dir);
49705b774   Christoph Hellwig   [PATCH] sanitize ...
3018
  	old_dentry = lookup_hash(&oldnd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
  	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 ...
3038
  	new_dentry = lookup_hash(&newnd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3039
3040
3041
3042
3043
3044
3045
  	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;
9079b1eb1   Dave Hansen   [PATCH] r/o bind ...
3046
3047
3048
  	error = mnt_want_write(oldnd.path.mnt);
  	if (error)
  		goto exit5;
be6d3e56a   Kentaro Takeda   introduce new LSM...
3049
3050
3051
3052
  	error = security_path_rename(&oldnd.path, old_dentry,
  				     &newnd.path, new_dentry);
  	if (error)
  		goto exit6;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3053
3054
  	error = vfs_rename(old_dir->d_inode, old_dentry,
  				   new_dir->d_inode, new_dentry);
be6d3e56a   Kentaro Takeda   introduce new LSM...
3055
  exit6:
9079b1eb1   Dave Hansen   [PATCH] r/o bind ...
3056
  	mnt_drop_write(oldnd.path.mnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3057
3058
3059
3060
3061
3062
3063
  exit5:
  	dput(new_dentry);
  exit4:
  	dput(old_dentry);
  exit3:
  	unlock_rename(new_dir, old_dir);
  exit2:
1d957f9bf   Jan Blunck   Introduce path_put()
3064
  	path_put(&newnd.path);
2ad94ae65   Al Viro   [PATCH] new (loca...
3065
  	putname(to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3066
  exit1:
1d957f9bf   Jan Blunck   Introduce path_put()
3067
  	path_put(&oldnd.path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3068
  	putname(from);
2ad94ae65   Al Viro   [PATCH] new (loca...
3069
  exit:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3070
3071
  	return error;
  }
a26eab240   Heiko Carstens   [CVE-2009-0029] S...
3072
  SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
3073
3074
3075
  {
  	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
  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...
3101
  	void *cookie;
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
3102
  	int res;
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3103

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3104
  	nd.depth = 0;
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3105
  	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
694a1764d   Marcin Slusarz   [patch 3/4] vfs: ...
3106
3107
3108
3109
3110
3111
3112
  	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
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
  }
  
  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 ...
3123
3124
  	char *kaddr;
  	struct page *page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3125
  	struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b185   Pekka Enberg   [PATCH] read_mapp...
3126
  	page = read_mapping_page(mapping, 0, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3127
  	if (IS_ERR(page))
6fe6900e1   Nick Piggin   mm: make read_cac...
3128
  		return (char*)page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3129
  	*ppage = page;
ebd09abbd   Duane Griffin   vfs: ensure page ...
3130
3131
3132
  	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
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
  }
  
  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...
3146
  void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3147
  {
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3148
  	struct page *page = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3149
  	nd_set_link(nd, page_getlink(dentry, &page));
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3150
  	return page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3151
  }
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3152
  void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3153
  {
cc314eef0   Linus Torvalds   Fix nasty ncpfs s...
3154
3155
3156
  	struct page *page = cookie;
  
  	if (page) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3157
3158
  		kunmap(page);
  		page_cache_release(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3159
3160
  	}
  }
54566b2c1   Nick Piggin   fs: symlink write...
3161
3162
3163
3164
  /*
   * 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
3165
3166
  {
  	struct address_space *mapping = inode->i_mapping;
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3167
  	struct page *page;
afddba49d   Nick Piggin   fs: introduce wri...
3168
  	void *fsdata;
beb497ab4   Dmitriy Monakhov   [PATCH] __page_sy...
3169
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3170
  	char *kaddr;
54566b2c1   Nick Piggin   fs: symlink write...
3171
3172
3173
  	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  	if (nofs)
  		flags |= AOP_FLAG_NOFS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3174

7e53cac41   NeilBrown   [PATCH] Honour AO...
3175
  retry:
afddba49d   Nick Piggin   fs: introduce wri...
3176
  	err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c1   Nick Piggin   fs: symlink write...
3177
  				flags, &page, &fsdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3178
  	if (err)
afddba49d   Nick Piggin   fs: introduce wri...
3179
  		goto fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3180
3181
3182
  	kaddr = kmap_atomic(page, KM_USER0);
  	memcpy(kaddr, symname, len-1);
  	kunmap_atomic(kaddr, KM_USER0);
afddba49d   Nick Piggin   fs: introduce wri...
3183
3184
3185
  
  	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  							page, fsdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3186
3187
  	if (err < 0)
  		goto fail;
afddba49d   Nick Piggin   fs: introduce wri...
3188
3189
  	if (err < len-1)
  		goto retry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3190
3191
  	mark_inode_dirty(inode);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3192
3193
3194
  fail:
  	return err;
  }
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3195
3196
3197
  int page_symlink(struct inode *inode, const char *symname, int len)
  {
  	return __page_symlink(inode, symname, len,
54566b2c1   Nick Piggin   fs: symlink write...
3198
  			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3199
  }
92e1d5be9   Arjan van de Ven   [PATCH] mark stru...
3200
  const struct inode_operations page_symlink_inode_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3201
3202
3203
3204
  	.readlink	= generic_readlink,
  	.follow_link	= page_follow_link_light,
  	.put_link	= page_put_link,
  };
2d8f30380   Al Viro   [PATCH] sanitize ...
3205
  EXPORT_SYMBOL(user_path_at);
cc53ce53c   David Howells   Add a dentry op t...
3206
  EXPORT_SYMBOL(follow_down_one);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3207
3208
3209
3210
3211
  EXPORT_SYMBOL(follow_down);
  EXPORT_SYMBOL(follow_up);
  EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  EXPORT_SYMBOL(getname);
  EXPORT_SYMBOL(lock_rename);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3212
3213
3214
3215
  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...
3216
  EXPORT_SYMBOL(__page_symlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3217
3218
  EXPORT_SYMBOL(page_symlink);
  EXPORT_SYMBOL(page_symlink_inode_operations);
d18114657   Al Viro   [PATCH] new helpe...
3219
  EXPORT_SYMBOL(kern_path);
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
3220
  EXPORT_SYMBOL(vfs_path_lookup);
f419a2e3b   Al Viro   [PATCH] kill name...
3221
  EXPORT_SYMBOL(inode_permission);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
  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);