Blame view

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

9c2c70392   Al Viro   ->permission() sa...
222
  	mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
e6305c43e   Al Viro   [PATCH] sanitize ...
223

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

d594e7ec4   Al Viro   massage generic_p...
272
273
274
275
276
277
278
279
280
  	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
281
282
  	/*
  	 * Read/write DACs are always overridable.
d594e7ec4   Al Viro   massage generic_p...
283
284
  	 * Executable DACs are overridable when there is
  	 * at least one exec bit set.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
  	 */
d594e7ec4   Al Viro   massage generic_p...
286
  	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
e795b7179   Serge E. Hallyn   userns: userns: c...
287
  		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
290
291
292
  			return 0;
  
  	/*
  	 * Searching includes executable on directories, else just read.
  	 */
7ea660014   Serge E. Hallyn   generic_permissio...
293
  	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec4   Al Viro   massage generic_p...
294
  	if (mask == MAY_READ)
e795b7179   Serge E. Hallyn   userns: userns: c...
295
  		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
296
297
298
299
  			return 0;
  
  	return -EACCES;
  }
3ddcd0569   Linus Torvalds   vfs: optimize ino...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  /*
   * 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
319
320
321
322
323
324
325
326
327
328
  /**
   * inode_permission  -  check for access rights to a given inode
   * @inode:	inode to check permission on
   * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
   *
   * 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.
   */
f419a2e3b   Al Viro   [PATCH] kill name...
329
  int inode_permission(struct inode *inode, int mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
  {
e6305c43e   Al Viro   [PATCH] sanitize ...
331
  	int retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
332

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

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

9f1fafee9   Al Viro   merge handle_reva...
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  	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...
512
513
514
515
  	if (likely(!(nd->flags & LOOKUP_JUMPED)))
  		return 0;
  
  	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
39159de2a   Jeff Layton   vfs: force reval ...
516
  		return 0;
16c2cd717   Al Viro   untangle the "nee...
517
518
519
520
  	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...
521
  	status = d_revalidate(dentry, nd);
39159de2a   Jeff Layton   vfs: force reval ...
522
523
  	if (status > 0)
  		return 0;
16c2cd717   Al Viro   untangle the "nee...
524
  	if (!status)
39159de2a   Jeff Layton   vfs: force reval ...
525
  		status = -ESTALE;
16c2cd717   Al Viro   untangle the "nee...
526

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

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

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

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

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

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

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

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

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

2a7378711   Al Viro   Cache root in nam...
983
984
  		if (nd->path.dentry == nd->root.dentry &&
  		    nd->path.mnt == nd->root.mnt) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
985
986
  			break;
  		}
4ac913785   Jan Blunck   Embed a struct pa...
987
  		if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd708   Al Viro   Clean follow_dotd...
988
989
  			/* rare case of legitimate dget_parent()... */
  			nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
990
991
992
  			dput(old);
  			break;
  		}
3088dd708   Al Viro   Clean follow_dotd...
993
  		if (!follow_up(&nd->path))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
994
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
995
  	}
79ed02261   Al Viro   switch follow_mou...
996
  	follow_mount(&nd->path);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
997
  	nd->inode = nd->path.dentry->d_inode;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
  /*
baa038907   Nick Piggin   fs: dentry alloca...
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
   * 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_...
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
   * 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
1053
1054
1055
1056
1057
   *  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 ...
1058
  			struct path *path, struct inode **inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1059
  {
4ac913785   Jan Blunck   Embed a struct pa...
1060
  	struct vfsmount *mnt = nd->path.mnt;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1061
  	struct dentry *dentry, *parent = nd->path.dentry;
5a18fff20   Al Viro   untangle do_lookup()
1062
1063
  	int need_reval = 1;
  	int status = 1;
9875cf806   David Howells   Add a dentry op t...
1064
  	int err;
3cac260ad   Al Viro   Take hash recalcu...
1065
  	/*
b04f784e5   Nick Piggin   fs: remove extra ...
1066
1067
1068
1069
  	 * 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 ...
1070
1071
  	if (nd->flags & LOOKUP_RCU) {
  		unsigned seq;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1072
1073
  		*inode = nd->inode;
  		dentry = __d_lookup_rcu(parent, name, &seq, inode);
5a18fff20   Al Viro   untangle do_lookup()
1074
1075
  		if (!dentry)
  			goto unlazy;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1076
1077
1078
  		/* 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 ...
1079
  		nd->seq = seq;
5a18fff20   Al Viro   untangle do_lookup()
1080

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

44396f4b5   Josef Bacik   fs: add a DCACHE_...
1105
1106
1107
1108
  	if (dentry && unlikely(d_need_lookup(dentry))) {
  		dput(dentry);
  		dentry = NULL;
  	}
5a18fff20   Al Viro   untangle do_lookup()
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
  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_...
1125
1126
1127
1128
1129
1130
1131
1132
1133
  		} 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()
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
  		}
  		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...
1150
  	}
5a18fff20   Al Viro   untangle do_lookup()
1151

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1325
1326
1327
1328
1329
  	/* 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...
1330
  		int type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1331

52094c8a0   Al Viro   take RCU-dependen...
1332
  		err = may_lookup(nd);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
   		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...
1347
1348
1349
  		type = LAST_NORM;
  		if (this.name[0] == '.') switch (this.len) {
  			case 2:
16c2cd717   Al Viro   untangle the "nee...
1350
  				if (this.name[1] == '.') {
fe479a580   Al Viro   merge component t...
1351
  					type = LAST_DOTDOT;
16c2cd717   Al Viro   untangle the "nee...
1352
1353
  					nd->flags |= LOOKUP_JUMPED;
  				}
fe479a580   Al Viro   merge component t...
1354
1355
1356
1357
  				break;
  			case 1:
  				type = LAST_DOT;
  		}
5a202bcd7   Al Viro   sanitize pathname...
1358
1359
  		if (likely(type == LAST_NORM)) {
  			struct dentry *parent = nd->path.dentry;
16c2cd717   Al Viro   untangle the "nee...
1360
  			nd->flags &= ~LOOKUP_JUMPED;
5a202bcd7   Al Viro   sanitize pathname...
1361
1362
1363
1364
1365
1366
1367
  			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...
1368

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1369
1370
1371
1372
1373
  		/* remove trailing slashes? */
  		if (!c)
  			goto last_component;
  		while (*++name == '/');
  		if (!*name)
b356379a0   Al Viro   Turn resolution o...
1374
  			goto last_component;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1375

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

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

e41f7d4ee   Al Viro   merge path_init a...
1445
1446
  			br_read_lock(vfsmount_lock);
  			rcu_read_lock();
c28cc3646   Nick Piggin   fs: fs_struct use...
1447

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

4ad5abb3d   Al Viro   no reason to keep...
1469
  			retval = inode_permission(dentry->d_inode, MAY_EXEC);
f52e0c113   Al Viro   New AT_... flag: ...
1470
1471
1472
  			if (retval)
  				goto fput_fail;
  		}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1473
1474
  
  		nd->path = file->f_path;
e41f7d4ee   Al Viro   merge path_init a...
1475
1476
  		if (flags & LOOKUP_RCU) {
  			if (fput_needed)
70e9b3571   Al Viro   get rid of nd->file
1477
  				*fp = file;
e41f7d4ee   Al Viro   merge path_init a...
1478
1479
1480
1481
1482
1483
1484
  			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 ...
1485
  	}
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1486

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

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

bd92d7fed   Al Viro   Make trailing sym...
1528
1529
  	if (unlikely(err))
  		return err;
ee0827cd6   Al Viro   sanitize path_wal...
1530
1531
  
  	current->total_link_count = 0;
bd92d7fed   Al Viro   Make trailing sym...
1532
1533
1534
  	err = link_path_walk(name, nd);
  
  	if (!err && !(flags & LOOKUP_PARENT)) {
bd92d7fed   Al Viro   Make trailing sym...
1535
1536
1537
1538
  		err = lookup_last(nd, &path);
  		while (err > 0) {
  			void *cookie;
  			struct path link = path;
bd92d7fed   Al Viro   Make trailing sym...
1539
  			nd->flags |= LOOKUP_PARENT;
574197e0d   Al Viro   tidy the trailing...
1540
  			err = follow_link(&link, nd, &cookie);
bd92d7fed   Al Viro   Make trailing sym...
1541
1542
  			if (!err)
  				err = lookup_last(nd, &path);
574197e0d   Al Viro   tidy the trailing...
1543
  			put_link(nd, &link, cookie);
bd92d7fed   Al Viro   Make trailing sym...
1544
1545
  		}
  	}
ee0827cd6   Al Viro   sanitize path_wal...
1546

9f1fafee9   Al Viro   merge handle_reva...
1547
1548
  	if (!err)
  		err = complete_walk(nd);
bd92d7fed   Al Viro   Make trailing sym...
1549
1550
1551
1552
  
  	if (!err && nd->flags & LOOKUP_DIRECTORY) {
  		if (!nd->inode->i_op->lookup) {
  			path_put(&nd->path);
bd23a539d   Al Viro   fix leaks in path...
1553
  			err = -ENOTDIR;
bd92d7fed   Al Viro   Make trailing sym...
1554
1555
  		}
  	}
16c2cd717   Al Viro   untangle the "nee...
1556

70e9b3571   Al Viro   get rid of nd->file
1557
1558
  	if (base)
  		fput(base);
ee0827cd6   Al Viro   sanitize path_wal...
1559

5b6ca027d   Al Viro   reduce vfs_path_l...
1560
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2a7378711   Al Viro   Cache root in nam...
1561
1562
1563
  		path_put(&nd->root);
  		nd->root.mnt = NULL;
  	}
bd92d7fed   Al Viro   Make trailing sym...
1564
  	return err;
ee0827cd6   Al Viro   sanitize path_wal...
1565
  }
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
1566

ee0827cd6   Al Viro   sanitize path_wal...
1567
1568
1569
1570
1571
1572
1573
1574
  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 ...
1575
1576
1577
1578
1579
1580
1581
  
  	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: ...
1582
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1583
  }
c9c6cac0c   Al Viro   kill path_lookup()
1584
  int kern_path_parent(const char *name, struct nameidata *nd)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
1585
  {
c9c6cac0c   Al Viro   kill path_lookup()
1586
  	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
1587
  }
d18114657   Al Viro   [PATCH] new helpe...
1588
1589
1590
1591
1592
1593
1594
1595
  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...
1596
1597
1598
1599
1600
1601
  /**
   * 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...
1602
   * @path: pointer to struct path to fill
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1603
1604
1605
   */
  int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  		    const char *name, unsigned int flags,
e0a012493   Al Viro   switch vfs_path_l...
1606
  		    struct path *path)
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
1607
  {
e0a012493   Al Viro   switch vfs_path_l...
1608
1609
1610
1611
1612
  	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...
1613
  	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
e0a012493   Al Viro   switch vfs_path_l...
1614
1615
1616
1617
  	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...
1618
  }
eead19115   Christoph Hellwig   partially fix up ...
1619
1620
  static struct dentry *__lookup_hash(struct qstr *name,
  		struct dentry *base, struct nameidata *nd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1621
  {
81fca4440   Christoph Hellwig   fs: move permissi...
1622
  	struct inode *inode = base->d_inode;
057f6c019   James Morris   security: prevent...
1623
  	struct dentry *dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1624
  	int err;
4ad5abb3d   Al Viro   no reason to keep...
1625
  	err = inode_permission(inode, MAY_EXEC);
81fca4440   Christoph Hellwig   fs: move permissi...
1626
1627
  	if (err)
  		return ERR_PTR(err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1628
1629
  
  	/*
b04f784e5   Nick Piggin   fs: remove extra ...
1630
1631
1632
  	 * 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...
1633
  	 */
b04f784e5   Nick Piggin   fs: remove extra ...
1634
  	dentry = d_lookup(base, name);
6e6b1bd1e   Al Viro   Kill cached_looku...
1635

44396f4b5   Josef Bacik   fs: add a DCACHE_...
1636
1637
1638
1639
1640
1641
1642
1643
1644
  	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...
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
  	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...
1663

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

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

2f9092e10   David Woodhouse   Fix i_mutex vs. r...
1695
  	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
6a96ba544   Al Viro   kill __lookup_one...
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
  	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...
1709
1710
1711
1712
1713
1714
1715
1716
1717
  	/*
  	 * 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 ...
1718

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

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

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

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1932
1933
1934
1935
  	/*
  	 * An append-only file must be opened in append mode for writing.
  	 */
  	if (IS_APPEND(inode)) {
8737c9305   Al Viro   Switch may_open()...
1936
  		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b5212   Al Viro   O_TRUNC open shou...
1937
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1938
  		if (flag & O_TRUNC)
7715b5212   Al Viro   O_TRUNC open shou...
1939
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1940
1941
1942
  	}
  
  	/* O_NOATIME can only be set by the owner or superuser */
2e1496707   Serge E. Hallyn   userns: rename is...
1943
  	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b5212   Al Viro   O_TRUNC open shou...
1944
  		return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1945
1946
1947
1948
  
  	/*
  	 * Ensure there are no outstanding leases on the file.
  	 */
b65a9cfc2   Al Viro   Untangling ima me...
1949
  	return break_lease(inode, flag);
7715b5212   Al Viro   O_TRUNC open shou...
1950
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1951

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

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

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

a1e28038d   Al Viro   pull the common p...
2059
  	mutex_lock(&dir->d_inode->i_mutex);
6c0d46c49   Al Viro   fold __open_namei...
2060
2061
2062
  	dentry = lookup_hash(nd);
  	error = PTR_ERR(dentry);
  	if (IS_ERR(dentry)) {
fb1cc555d   Al Viro   gut do_filp_open(...
2063
2064
2065
  		mutex_unlock(&dir->d_inode->i_mutex);
  		goto exit;
  	}
6c0d46c49   Al Viro   fold __open_namei...
2066
2067
  	path->dentry = dentry;
  	path->mnt = nd->path.mnt;
fb1cc555d   Al Viro   gut do_filp_open(...
2068
  	/* Negative dentry, just create the file */
6c0d46c49   Al Viro   fold __open_namei...
2069
2070
2071
2072
  	if (!dentry->d_inode) {
  		int mode = op->mode;
  		if (!IS_POSIXACL(dir->d_inode))
  			mode &= ~current_umask();
fb1cc555d   Al Viro   gut do_filp_open(...
2073
2074
  		/*
  		 * This write is needed to ensure that a
6c0d46c49   Al Viro   fold __open_namei...
2075
  		 * rw->ro transition does not occur between
fb1cc555d   Al Viro   gut do_filp_open(...
2076
2077
2078
2079
2080
2081
2082
  		 * 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...
2083
  		want_write = 1;
9b44f1b39   Al Viro   move may_open() f...
2084
  		/* Don't check for write permission, don't truncate */
ca344a894   Al Viro   do_last: unify ma...
2085
  		open_flag &= ~O_TRUNC;
6c0d46c49   Al Viro   fold __open_namei...
2086
  		will_truncate = 0;
bcda76524   Al Viro   Allow O_PATH for ...
2087
  		acc_mode = MAY_OPEN;
6c0d46c49   Al Viro   fold __open_namei...
2088
2089
2090
2091
2092
2093
2094
2095
2096
  		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...
2097
  		goto common;
fb1cc555d   Al Viro   gut do_filp_open(...
2098
2099
2100
2101
2102
2103
2104
2105
2106
  	}
  
  	/*
  	 * It already exists.
  	 */
  	mutex_unlock(&dir->d_inode->i_mutex);
  	audit_inode(pathname, path->dentry);
  
  	error = -EEXIST;
ca344a894   Al Viro   do_last: unify ma...
2107
  	if (open_flag & O_EXCL)
fb1cc555d   Al Viro   gut do_filp_open(...
2108
  		goto exit_dput;
9875cf806   David Howells   Add a dentry op t...
2109
2110
2111
  	error = follow_managed(path, nd->flags);
  	if (error < 0)
  		goto exit_dput;
fb1cc555d   Al Viro   gut do_filp_open(...
2112
2113
2114
2115
  
  	error = -ENOENT;
  	if (!path->dentry->d_inode)
  		goto exit_dput;
9e67f3616   Al Viro   Kill is_link argu...
2116
2117
  
  	if (path->dentry->d_inode->i_op->follow_link)
fb1cc555d   Al Viro   gut do_filp_open(...
2118
  		return NULL;
fb1cc555d   Al Viro   gut do_filp_open(...
2119
2120
  
  	path_to_nameidata(path, nd);
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2121
  	nd->inode = path->dentry->d_inode;
fb1cc555d   Al Viro   gut do_filp_open(...
2122
  	error = -EISDIR;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2123
  	if (S_ISDIR(nd->inode->i_mode))
fb1cc555d   Al Viro   gut do_filp_open(...
2124
  		goto exit;
67ee3ad21   Al Viro   Pull handling of ...
2125
  ok:
6c0d46c49   Al Viro   fold __open_namei...
2126
2127
  	if (!S_ISREG(nd->inode->i_mode))
  		will_truncate = 0;
0f9d1a10c   Al Viro   expand finish_ope...
2128
2129
2130
2131
  	if (will_truncate) {
  		error = mnt_want_write(nd->path.mnt);
  		if (error)
  			goto exit;
ca344a894   Al Viro   do_last: unify ma...
2132
  		want_write = 1;
0f9d1a10c   Al Viro   expand finish_ope...
2133
  	}
ca344a894   Al Viro   do_last: unify ma...
2134
  common:
bcda76524   Al Viro   Allow O_PATH for ...
2135
  	error = may_open(&nd->path, acc_mode, open_flag);
ca344a894   Al Viro   do_last: unify ma...
2136
  	if (error)
0f9d1a10c   Al Viro   expand finish_ope...
2137
  		goto exit;
0f9d1a10c   Al Viro   expand finish_ope...
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
  	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...
2155
2156
  out:
  	if (want_write)
0f9d1a10c   Al Viro   expand finish_ope...
2157
2158
  		mnt_drop_write(nd->path.mnt);
  	path_put(&nd->path);
fb1cc555d   Al Viro   gut do_filp_open(...
2159
2160
2161
2162
2163
2164
2165
  	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...
2166
2167
  	filp = ERR_PTR(error);
  	goto out;
fb1cc555d   Al Viro   gut do_filp_open(...
2168
  }
13aab428a   Al Viro   separate -ESTALE/...
2169
  static struct file *path_openat(int dfd, const char *pathname,
73d049a40   Al Viro   open-style analog...
2170
  		struct nameidata *nd, const struct open_flags *op, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2171
  {
fe2d35ff0   Al Viro   switch non-create...
2172
  	struct file *base = NULL;
4a3fd211c   Dave Hansen   [PATCH] r/o bind ...
2173
  	struct file *filp;
9850c0565   Al Viro   Fix the -ESTALE h...
2174
  	struct path path;
13aab428a   Al Viro   separate -ESTALE/...
2175
  	int error;
31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2176
2177
2178
2179
  
  	filp = get_empty_filp();
  	if (!filp)
  		return ERR_PTR(-ENFILE);
47c805dc2   Al Viro   switch do_filp_op...
2180
  	filp->f_flags = op->open_flag;
73d049a40   Al Viro   open-style analog...
2181
2182
2183
  	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 ...
2184

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

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

73d049a40   Al Viro   open-style analog...
2194
  	filp = do_last(nd, &path, op, pathname);
806b681cb   Al Viro   Turn do_link spag...
2195
  	while (unlikely(!filp)) { /* trailing symlink */
7b9337aaf   Nick Piggin   fs: namei fix ->p...
2196
  		struct path link = path;
def4af30c   Al Viro   Get rid of symlin...
2197
  		void *cookie;
574197e0d   Al Viro   tidy the trailing...
2198
  		if (!(nd->flags & LOOKUP_FOLLOW)) {
73d049a40   Al Viro   open-style analog...
2199
2200
  			path_put_conditional(&path, nd);
  			path_put(&nd->path);
40b39136f   Al Viro   path_openat: clea...
2201
2202
2203
  			filp = ERR_PTR(-ELOOP);
  			break;
  		}
73d049a40   Al Viro   open-style analog...
2204
2205
  		nd->flags |= LOOKUP_PARENT;
  		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
574197e0d   Al Viro   tidy the trailing...
2206
  		error = follow_link(&link, nd, &cookie);
c3e380b0b   Al Viro   Collect "operatio...
2207
  		if (unlikely(error))
f1afe9efc   Al Viro   clean up the fail...
2208
  			filp = ERR_PTR(error);
c3e380b0b   Al Viro   Collect "operatio...
2209
  		else
73d049a40   Al Viro   open-style analog...
2210
  			filp = do_last(nd, &path, op, pathname);
574197e0d   Al Viro   tidy the trailing...
2211
  		put_link(nd, &link, cookie);
806b681cb   Al Viro   Turn do_link spag...
2212
  	}
10fa8e62f   Al Viro   Unify exits in O_...
2213
  out:
73d049a40   Al Viro   open-style analog...
2214
2215
  	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  		path_put(&nd->root);
fe2d35ff0   Al Viro   switch non-create...
2216
2217
  	if (base)
  		fput(base);
73d049a40   Al Viro   open-style analog...
2218
  	release_open_intent(nd);
10fa8e62f   Al Viro   Unify exits in O_...
2219
  	return filp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2220

31e6b01f4   Nick Piggin   fs: rcu-walk for ...
2221
  out_filp:
806b681cb   Al Viro   Turn do_link spag...
2222
  	filp = ERR_PTR(error);
10fa8e62f   Al Viro   Unify exits in O_...
2223
  	goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2224
  }
13aab428a   Al Viro   separate -ESTALE/...
2225
2226
2227
  struct file *do_filp_open(int dfd, const char *pathname,
  		const struct open_flags *op, int flags)
  {
73d049a40   Al Viro   open-style analog...
2228
  	struct nameidata nd;
13aab428a   Al Viro   separate -ESTALE/...
2229
  	struct file *filp;
73d049a40   Al Viro   open-style analog...
2230
  	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
13aab428a   Al Viro   separate -ESTALE/...
2231
  	if (unlikely(filp == ERR_PTR(-ECHILD)))
73d049a40   Al Viro   open-style analog...
2232
  		filp = path_openat(dfd, pathname, &nd, op, flags);
13aab428a   Al Viro   separate -ESTALE/...
2233
  	if (unlikely(filp == ERR_PTR(-ESTALE)))
73d049a40   Al Viro   open-style analog...
2234
  		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
13aab428a   Al Viro   separate -ESTALE/...
2235
2236
  	return filp;
  }
73d049a40   Al Viro   open-style analog...
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
  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 ...
2247
  	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
73d049a40   Al Viro   open-style analog...
2248
2249
2250
2251
2252
2253
2254
2255
2256
  		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()
2257
  struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2258
  {
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2259
  	struct dentry *dentry = ERR_PTR(-EEXIST);
ed75e95de   Al Viro   kill lookup_create()
2260
2261
2262
2263
  	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
2264

c663e5d80   Christoph Hellwig   [PATCH] add some ...
2265
2266
2267
2268
  	/*
  	 * Yucky last component or no last component at all?
  	 * (foo/., foo/.., /////)
  	 */
ed75e95de   Al Viro   kill lookup_create()
2269
2270
2271
2272
2273
  	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 ...
2274
2275
2276
2277
  
  	/*
  	 * Do the final lookup.
  	 */
ed75e95de   Al Viro   kill lookup_create()
2278
2279
  	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
2280
2281
  	if (IS_ERR(dentry))
  		goto fail;
c663e5d80   Christoph Hellwig   [PATCH] add some ...
2282

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

dae6ad8f3   Al Viro   new helpers: kern...
2368
2369
2370
  	dentry = user_path_create(dfd, filename, &path, 0);
  	if (IS_ERR(dentry))
  		return PTR_ERR(dentry);
2ad94ae65   Al Viro   [PATCH] new (loca...
2371

dae6ad8f3   Al Viro   new helpers: kern...
2372
  	if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d5   Al Viro   New helper - curr...
2373
  		mode &= ~current_umask();
463c31972   Dave Hansen   [PATCH] r/o bind ...
2374
2375
2376
  	error = may_mknod(mode);
  	if (error)
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
2377
  	error = mnt_want_write(path.mnt);
463c31972   Dave Hansen   [PATCH] r/o bind ...
2378
2379
  	if (error)
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
2380
  	error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2381
2382
  	if (error)
  		goto out_drop_write;
463c31972   Dave Hansen   [PATCH] r/o bind ...
2383
  	switch (mode & S_IFMT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2384
  		case 0: case S_IFREG:
dae6ad8f3   Al Viro   new helpers: kern...
2385
  			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2386
2387
  			break;
  		case S_IFCHR: case S_IFBLK:
dae6ad8f3   Al Viro   new helpers: kern...
2388
  			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2389
2390
2391
  					new_decode_dev(dev));
  			break;
  		case S_IFIFO: case S_IFSOCK:
dae6ad8f3   Al Viro   new helpers: kern...
2392
  			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2393
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2394
  	}
be6d3e56a   Kentaro Takeda   introduce new LSM...
2395
  out_drop_write:
dae6ad8f3   Al Viro   new helpers: kern...
2396
  	mnt_drop_write(path.mnt);
463c31972   Dave Hansen   [PATCH] r/o bind ...
2397
2398
  out_dput:
  	dput(dentry);
dae6ad8f3   Al Viro   new helpers: kern...
2399
2400
  	mutex_unlock(&path.dentry->d_inode->i_mutex);
  	path_put(&path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2401
2402
2403
  
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
2404
  SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2405
2406
2407
  {
  	return sys_mknodat(AT_FDCWD, filename, mode, dev);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2408
2409
  int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  {
a95164d97   Miklos Szeredi   [patch 3/4] vfs: ...
2410
  	int error = may_create(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2411
2412
2413
  
  	if (error)
  		return error;
acfa4380e   Al Viro   inode->i_op is ne...
2414
  	if (!dir->i_op->mkdir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2415
2416
2417
2418
2419
2420
  		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
2421
  	error = dir->i_op->mkdir(dir, dentry, mode);
a74574aaf   Stephen Smalley   [PATCH] Remove se...
2422
  	if (!error)
f38aa9422   Amy Griffis   [PATCH] Pass dent...
2423
  		fsnotify_mkdir(dir, dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2424
2425
  	return error;
  }
2e4d0924e   Heiko Carstens   [CVE-2009-0029] S...
2426
  SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2427
  {
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2428
  	struct dentry *dentry;
dae6ad8f3   Al Viro   new helpers: kern...
2429
2430
  	struct path path;
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2431

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

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

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

dae6ad8f3   Al Viro   new helpers: kern...
2698
  	dentry = user_path_create(newdfd, newname, &path, 0);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2699
2700
  	error = PTR_ERR(dentry);
  	if (IS_ERR(dentry))
dae6ad8f3   Al Viro   new helpers: kern...
2701
  		goto out_putname;
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2702

dae6ad8f3   Al Viro   new helpers: kern...
2703
  	error = mnt_want_write(path.mnt);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
2704
2705
  	if (error)
  		goto out_dput;
dae6ad8f3   Al Viro   new helpers: kern...
2706
  	error = security_path_symlink(&path, dentry, from);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2707
2708
  	if (error)
  		goto out_drop_write;
dae6ad8f3   Al Viro   new helpers: kern...
2709
  	error = vfs_symlink(path.dentry->d_inode, dentry, from);
be6d3e56a   Kentaro Takeda   introduce new LSM...
2710
  out_drop_write:
dae6ad8f3   Al Viro   new helpers: kern...
2711
  	mnt_drop_write(path.mnt);
75c3f29de   Dave Hansen   [PATCH] r/o bind ...
2712
  out_dput:
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2713
  	dput(dentry);
dae6ad8f3   Al Viro   new helpers: kern...
2714
2715
  	mutex_unlock(&path.dentry->d_inode->i_mutex);
  	path_put(&path);
6902d925d   Dave Hansen   [PATCH] r/o bind ...
2716
  out_putname:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2717
2718
2719
  	putname(from);
  	return error;
  }
3480b2574   Heiko Carstens   [CVE-2009-0029] S...
2720
  SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d5   Ulrich Drepper   [PATCH] vfs: *at ...
2721
2722
2723
  {
  	return sys_symlinkat(oldname, AT_FDCWD, newname);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2724
2725
2726
2727
2728
2729
2730
  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: ...
2731
  	error = may_create(dir, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
  	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...
2743
  	if (!dir->i_op->link)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2744
  		return -EPERM;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
2745
  	if (S_ISDIR(inode->i_mode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2746
2747
2748
2749
2750
  		return -EPERM;
  
  	error = security_inode_link(old_dentry, dir, new_dentry);
  	if (error)
  		return error;
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
2751
  	mutex_lock(&inode->i_mutex);
aae8a97d3   Aneesh Kumar K.V   fs: Don't allow t...
2752
2753
2754
2755
2756
  	/* 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: ...
2757
  	mutex_unlock(&inode->i_mutex);
e31e14ec3   Stephen Smalley   [PATCH] remove th...
2758
  	if (!error)
7e79eedb3   Tetsuo Handa   [patch 4/5] vfs: ...
2759
  		fsnotify_link(dir, inode, new_dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
  	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...
2772
2773
  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
2774
2775
  {
  	struct dentry *new_dentry;
dae6ad8f3   Al Viro   new helpers: kern...
2776
  	struct path old_path, new_path;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
2777
  	int how = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2778
  	int error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2779

11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
2780
  	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e16   Ulrich Drepper   [PATCH] flags par...
2781
  		return -EINVAL;
11a7b371b   Aneesh Kumar K.V   fs: allow AT_EMPT...
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
  	/*
  	 * 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...
2795

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

2ad94ae65   Al Viro   [PATCH] new (loca...
2977
  	error = user_path_parent(olddfd, oldname, &oldnd, &from);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2978
2979
  	if (error)
  		goto exit;
2ad94ae65   Al Viro   [PATCH] new (loca...
2980
  	error = user_path_parent(newdfd, newname, &newnd, &to);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2981
2982
2983
2984
  	if (error)
  		goto exit1;
  
  	error = -EXDEV;
4ac913785   Jan Blunck   Embed a struct pa...
2985
  	if (oldnd.path.mnt != newnd.path.mnt)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2986
  		goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
2987
  	old_dir = oldnd.path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2988
2989
2990
  	error = -EBUSY;
  	if (oldnd.last_type != LAST_NORM)
  		goto exit2;
4ac913785   Jan Blunck   Embed a struct pa...
2991
  	new_dir = newnd.path.dentry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2992
2993
  	if (newnd.last_type != LAST_NORM)
  		goto exit2;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
2994
2995
  	oldnd.flags &= ~LOOKUP_PARENT;
  	newnd.flags &= ~LOOKUP_PARENT;
4e9ed2f85   OGAWA Hirofumi   [PATCH vfs-2.6 6/...
2996
  	newnd.flags |= LOOKUP_RENAME_TARGET;
0612d9fb2   OGAWA Hirofumi   [PATCH vfs-2.6 5/...
2997

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

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

7e53cac41   NeilBrown   [PATCH] Honour AO...
3156
  retry:
afddba49d   Nick Piggin   fs: introduce wri...
3157
  	err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c1   Nick Piggin   fs: symlink write...
3158
  				flags, &page, &fsdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3159
  	if (err)
afddba49d   Nick Piggin   fs: introduce wri...
3160
  		goto fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3161
3162
3163
  	kaddr = kmap_atomic(page, KM_USER0);
  	memcpy(kaddr, symname, len-1);
  	kunmap_atomic(kaddr, KM_USER0);
afddba49d   Nick Piggin   fs: introduce wri...
3164
3165
3166
  
  	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  							page, fsdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3167
3168
  	if (err < 0)
  		goto fail;
afddba49d   Nick Piggin   fs: introduce wri...
3169
3170
  	if (err < len-1)
  		goto retry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3171
3172
  	mark_inode_dirty(inode);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3173
3174
3175
  fail:
  	return err;
  }
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3176
3177
3178
  int page_symlink(struct inode *inode, const char *symname, int len)
  {
  	return __page_symlink(inode, symname, len,
54566b2c1   Nick Piggin   fs: symlink write...
3179
  			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2e   Kirill Korotaev   [PATCH] ext3: ext...
3180
  }
92e1d5be9   Arjan van de Ven   [PATCH] mark stru...
3181
  const struct inode_operations page_symlink_inode_operations = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3182
3183
3184
3185
  	.readlink	= generic_readlink,
  	.follow_link	= page_follow_link_light,
  	.put_link	= page_put_link,
  };
2d8f30380   Al Viro   [PATCH] sanitize ...
3186
  EXPORT_SYMBOL(user_path_at);
cc53ce53c   David Howells   Add a dentry op t...
3187
  EXPORT_SYMBOL(follow_down_one);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3188
3189
3190
3191
3192
  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
3193
3194
3195
3196
  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...
3197
  EXPORT_SYMBOL(__page_symlink);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3198
3199
  EXPORT_SYMBOL(page_symlink);
  EXPORT_SYMBOL(page_symlink_inode_operations);
d18114657   Al Viro   [PATCH] new helpe...
3200
  EXPORT_SYMBOL(kern_path);
16f182002   Josef 'Jeff' Sipek   fs: introduce vfs...
3201
  EXPORT_SYMBOL(vfs_path_lookup);
f419a2e3b   Al Viro   [PATCH] kill name...
3202
  EXPORT_SYMBOL(inode_permission);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
  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);