1 /*
2  *  linux/fs/namei.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6 
7 /*
8  * Some corrections by tytso.
9  */
10 
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12  * lookup logic.
13  */
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15  */
16 
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/fs.h>
20 #include <linux/quotaops.h>
21 #include <linux/pagemap.h>
22 #include <linux/dnotify.h>
23 #include <linux/smp_lock.h>
24 #include <linux/personality.h>
25 
26 #include <asm/namei.h>
27 #include <asm/uaccess.h>
28 
29 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
30 
31 /* [Feb-1997 T. Schoebel-Theuer]
32  * Fundamental changes in the pathname lookup mechanisms (namei)
33  * were necessary because of omirr.  The reason is that omirr needs
34  * to know the _real_ pathname, not the user-supplied one, in case
35  * of symlinks (and also when transname replacements occur).
36  *
37  * The new code replaces the old recursive symlink resolution with
38  * an iterative one (in case of non-nested symlink chains).  It does
39  * this with calls to <fs>_follow_link().
40  * As a side effect, dir_namei(), _namei() and follow_link() are now
41  * replaced with a single function lookup_dentry() that can handle all
42  * the special cases of the former code.
43  *
44  * With the new dcache, the pathname is stored at each inode, at least as
45  * long as the refcount of the inode is positive.  As a side effect, the
46  * size of the dcache depends on the inode cache and thus is dynamic.
47  *
48  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
49  * resolution to correspond with current state of the code.
50  *
51  * Note that the symlink resolution is not *completely* iterative.
52  * There is still a significant amount of tail- and mid- recursion in
53  * the algorithm.  Also, note that <fs>_readlink() is not used in
54  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
55  * may return different results than <fs>_follow_link().  Many virtual
56  * filesystems (including /proc) exhibit this behavior.
57  */
58 
59 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
60  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
61  * and the name already exists in form of a symlink, try to create the new
62  * name indicated by the symlink. The old code always complained that the
63  * name already exists, due to not following the symlink even if its target
64  * is nonexistent.  The new semantics affects also mknod() and link() when
65  * the name is a symlink pointing to a non-existant name.
66  *
67  * I don't know which semantics is the right one, since I have no access
68  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
69  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
70  * "old" one. Personally, I think the new semantics is much more logical.
71  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
72  * file does succeed in both HP-UX and SunOs, but not in Solaris
73  * and in the old Linux semantics.
74  */
75 
76 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
77  * semantics.  See the comments in "open_namei" and "do_link" below.
78  *
79  * [10-Sep-98 Alan Modra] Another symlink change.
80  */
81 
82 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
83  *	inside the path - always follow.
84  *	in the last component in creation/removal/renaming - never follow.
85  *	if LOOKUP_FOLLOW passed - follow.
86  *	if the pathname has trailing slashes - follow.
87  *	otherwise - don't follow.
88  * (applied in that order).
89  *
90  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
91  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
92  * During the 2.4 we need to fix the userland stuff depending on it -
93  * hopefully we will be able to get rid of that wart in 2.5. So far only
94  * XEmacs seems to be relying on it...
95  */
96 
97 /* In order to reduce some races, while at the same time doing additional
98  * checking and hopefully speeding things up, we copy filenames to the
99  * kernel data space before using them..
100  *
101  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
102  * PATH_MAX includes the nul terminator --RR.
103  */
do_getname(const char * filename,char * page)104 static inline int do_getname(const char *filename, char *page)
105 {
106 	int retval;
107 	unsigned long len = PATH_MAX;
108 
109 	if ((unsigned long) filename >= TASK_SIZE) {
110 		if (!segment_eq(get_fs(), KERNEL_DS))
111 			return -EFAULT;
112 	} else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
113 		len = TASK_SIZE - (unsigned long) filename;
114 
115 	retval = strncpy_from_user((char *)page, filename, len);
116 	if (retval > 0) {
117 		if (retval < len)
118 			return 0;
119 		return -ENAMETOOLONG;
120 	} else if (!retval)
121 		retval = -ENOENT;
122 	return retval;
123 }
124 
getname(const char * filename)125 char * getname(const char * filename)
126 {
127 	char *tmp, *result;
128 
129 	result = ERR_PTR(-ENOMEM);
130 	tmp = __getname();
131 	if (tmp)  {
132 		int retval = do_getname(filename, tmp);
133 
134 		result = tmp;
135 		if (retval < 0) {
136 			putname(tmp);
137 			result = ERR_PTR(retval);
138 		}
139 	}
140 	return result;
141 }
142 
143 /*
144  *	vfs_permission()
145  *
146  * is used to check for read/write/execute permissions on a file.
147  * We use "fsuid" for this, letting us set arbitrary permissions
148  * for filesystem access without changing the "normal" uids which
149  * are used for other things..
150  */
vfs_permission(struct inode * inode,int mask)151 int vfs_permission(struct inode * inode, int mask)
152 {
153 	umode_t			mode = inode->i_mode;
154 
155 	if (mask & MAY_WRITE) {
156 		/*
157 		 * Nobody gets write access to a read-only fs.
158 		 */
159 		if (IS_RDONLY(inode) &&
160 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
161 			return -EROFS;
162 
163 		/*
164 		 * Nobody gets write access to an immutable file.
165 		 */
166 		if (IS_IMMUTABLE(inode))
167 			return -EACCES;
168 	}
169 
170 	if (current->fsuid == inode->i_uid)
171 		mode >>= 6;
172 	else if (in_group_p(inode->i_gid))
173 		mode >>= 3;
174 
175 	/*
176 	 * If the DACs are ok we don't need any capability check.
177 	 */
178 	if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
179 		return 0;
180 
181 	/*
182 	 * Read/write DACs are always overridable.
183 	 * Executable DACs are overridable if at least one exec bit is set.
184 	 */
185 	if ((mask & (MAY_READ|MAY_WRITE)) || (inode->i_mode & S_IXUGO))
186 		if (capable(CAP_DAC_OVERRIDE))
187 			return 0;
188 
189 	/*
190 	 * Searching includes executable on directories, else just read.
191 	 */
192 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
193 		if (capable(CAP_DAC_READ_SEARCH))
194 			return 0;
195 
196 	return -EACCES;
197 }
198 
permission(struct inode * inode,int mask)199 int permission(struct inode * inode,int mask)
200 {
201 	if (inode->i_op && inode->i_op->permission) {
202 		int retval;
203 		lock_kernel();
204 		retval = inode->i_op->permission(inode, mask);
205 		unlock_kernel();
206 		return retval;
207 	}
208 	return vfs_permission(inode, mask);
209 }
210 
211 /*
212  * get_write_access() gets write permission for a file.
213  * put_write_access() releases this write permission.
214  * This is used for regular files.
215  * We cannot support write (and maybe mmap read-write shared) accesses and
216  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
217  * can have the following values:
218  * 0: no writers, no VM_DENYWRITE mappings
219  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
220  * > 0: (i_writecount) users are writing to the file.
221  *
222  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
223  * except for the cases where we don't hold i_writecount yet. Then we need to
224  * use {get,deny}_write_access() - these functions check the sign and refuse
225  * to do the change if sign is wrong. Exclusion between them is provided by
226  * spinlock (arbitration_lock) and I'll rip the second arsehole to the first
227  * who will try to move it in struct inode - just leave it here.
228  */
229 static spinlock_t arbitration_lock = SPIN_LOCK_UNLOCKED;
get_write_access(struct inode * inode)230 int get_write_access(struct inode * inode)
231 {
232 	spin_lock(&arbitration_lock);
233 	if (atomic_read(&inode->i_writecount) < 0) {
234 		spin_unlock(&arbitration_lock);
235 		return -ETXTBSY;
236 	}
237 	atomic_inc(&inode->i_writecount);
238 	spin_unlock(&arbitration_lock);
239 	return 0;
240 }
deny_write_access(struct file * file)241 int deny_write_access(struct file * file)
242 {
243 	spin_lock(&arbitration_lock);
244 	if (atomic_read(&file->f_dentry->d_inode->i_writecount) > 0) {
245 		spin_unlock(&arbitration_lock);
246 		return -ETXTBSY;
247 	}
248 	atomic_dec(&file->f_dentry->d_inode->i_writecount);
249 	spin_unlock(&arbitration_lock);
250 	return 0;
251 }
252 
path_release(struct nameidata * nd)253 void path_release(struct nameidata *nd)
254 {
255 	dput(nd->dentry);
256 	mntput(nd->mnt);
257 }
258 
259 /*
260  * Internal lookup() using the new generic dcache.
261  * SMP-safe
262  */
cached_lookup(struct dentry * parent,struct qstr * name,int flags)263 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, int flags)
264 {
265 	struct dentry * dentry = d_lookup(parent, name);
266 
267 	if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
268 		if (!dentry->d_op->d_revalidate(dentry, flags) && !d_invalidate(dentry)) {
269 			dput(dentry);
270 			dentry = NULL;
271 		}
272 	}
273 	return dentry;
274 }
275 
276 /*
277  * This is called when everything else fails, and we actually have
278  * to go to the low-level filesystem to find out what we should do..
279  *
280  * We get the directory semaphore, and after getting that we also
281  * make sure that nobody added the entry to the dcache in the meantime..
282  * SMP-safe
283  */
real_lookup(struct dentry * parent,struct qstr * name,int flags)284 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, int flags)
285 {
286 	struct dentry * result;
287 	struct inode *dir = parent->d_inode;
288 
289 	down(&dir->i_sem);
290 	/*
291 	 * First re-do the cached lookup just in case it was created
292 	 * while we waited for the directory semaphore..
293 	 *
294 	 * FIXME! This could use version numbering or similar to
295 	 * avoid unnecessary cache lookups.
296 	 */
297 	result = d_lookup(parent, name);
298 	if (!result) {
299 		struct dentry *dentry;
300 
301 		/* Don't create child dentry for a dead directory. */
302 		result = ERR_PTR(-ENOENT);
303 		if (IS_DEADDIR(dir))
304 			goto out_unlock;
305 
306 		dentry = d_alloc(parent, name);
307 		result = ERR_PTR(-ENOMEM);
308 		if (dentry) {
309 			lock_kernel();
310 			result = dir->i_op->lookup(dir, dentry);
311 			unlock_kernel();
312 			if (result)
313 				dput(dentry);
314 			else
315 				result = dentry;
316 		}
317 	out_unlock:
318 		up(&dir->i_sem);
319 		return result;
320 	}
321 
322 	/*
323 	 * Uhhuh! Nasty case: the cache was re-populated while
324 	 * we waited on the semaphore. Need to revalidate.
325 	 */
326 	up(&dir->i_sem);
327 	if (result->d_op && result->d_op->d_revalidate) {
328 		if (!result->d_op->d_revalidate(result, flags) && !d_invalidate(result)) {
329 			dput(result);
330 			result = ERR_PTR(-ENOENT);
331 		}
332 	}
333 	return result;
334 }
335 
336 /*
337  * This limits recursive symlink follows to 5, while
338  * limiting consecutive symlinks to 40.
339  *
340  * Without that kind of total limit, nasty chains of consecutive
341  * symlinks can cause almost arbitrarily long lookups.
342  */
do_follow_link(struct dentry * dentry,struct nameidata * nd)343 static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
344 {
345 	int err;
346 	if (current->link_count >= 5)
347 		goto loop;
348 	if (current->total_link_count >= 40)
349 		goto loop;
350 	if (current->need_resched) {
351 		current->state = TASK_RUNNING;
352 		schedule();
353 	}
354 	current->link_count++;
355 	current->total_link_count++;
356 	UPDATE_ATIME(dentry->d_inode);
357 	err = dentry->d_inode->i_op->follow_link(dentry, nd);
358 	current->link_count--;
359 	return err;
360 loop:
361 	path_release(nd);
362 	return -ELOOP;
363 }
364 
__follow_up(struct vfsmount ** mnt,struct dentry ** base)365 static inline int __follow_up(struct vfsmount **mnt, struct dentry **base)
366 {
367 	struct vfsmount *parent;
368 	struct dentry *dentry;
369 	spin_lock(&dcache_lock);
370 	parent=(*mnt)->mnt_parent;
371 	if (parent == *mnt) {
372 		spin_unlock(&dcache_lock);
373 		return 0;
374 	}
375 	mntget(parent);
376 	dentry=dget((*mnt)->mnt_mountpoint);
377 	spin_unlock(&dcache_lock);
378 	dput(*base);
379 	*base = dentry;
380 	mntput(*mnt);
381 	*mnt = parent;
382 	return 1;
383 }
384 
follow_up(struct vfsmount ** mnt,struct dentry ** dentry)385 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
386 {
387 	return __follow_up(mnt, dentry);
388 }
389 
__follow_down(struct vfsmount ** mnt,struct dentry ** dentry)390 static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
391 {
392 	struct vfsmount *mounted;
393 
394 	spin_lock(&dcache_lock);
395 	mounted = lookup_mnt(*mnt, *dentry);
396 	if (mounted) {
397 		*mnt = mntget(mounted);
398 		spin_unlock(&dcache_lock);
399 		dput(*dentry);
400 		mntput(mounted->mnt_parent);
401 		*dentry = dget(mounted->mnt_root);
402 		return 1;
403 	}
404 	spin_unlock(&dcache_lock);
405 	return 0;
406 }
407 
follow_down(struct vfsmount ** mnt,struct dentry ** dentry)408 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
409 {
410 	return __follow_down(mnt,dentry);
411 }
412 
follow_dotdot(struct nameidata * nd)413 static inline void follow_dotdot(struct nameidata *nd)
414 {
415 	while(1) {
416 		struct vfsmount *parent;
417 		struct dentry *dentry;
418 		read_lock(&current->fs->lock);
419 		if (nd->dentry == current->fs->root &&
420 		    nd->mnt == current->fs->rootmnt)  {
421 			read_unlock(&current->fs->lock);
422 			break;
423 		}
424 		read_unlock(&current->fs->lock);
425 		spin_lock(&dcache_lock);
426 		if (nd->dentry != nd->mnt->mnt_root) {
427 			dentry = dget(nd->dentry->d_parent);
428 			spin_unlock(&dcache_lock);
429 			dput(nd->dentry);
430 			nd->dentry = dentry;
431 			break;
432 		}
433 		parent=nd->mnt->mnt_parent;
434 		if (parent == nd->mnt) {
435 			spin_unlock(&dcache_lock);
436 			break;
437 		}
438 		mntget(parent);
439 		dentry=dget(nd->mnt->mnt_mountpoint);
440 		spin_unlock(&dcache_lock);
441 		dput(nd->dentry);
442 		nd->dentry = dentry;
443 		mntput(nd->mnt);
444 		nd->mnt = parent;
445 	}
446 	while (d_mountpoint(nd->dentry) && __follow_down(&nd->mnt, &nd->dentry))
447 		;
448 }
449 
450 /*
451  * Name resolution.
452  *
453  * This is the basic name resolution function, turning a pathname
454  * into the final dentry.
455  *
456  * We expect 'base' to be positive and a directory.
457  */
link_path_walk(const char * name,struct nameidata * nd)458 int fastcall link_path_walk(const char * name, struct nameidata *nd)
459 {
460 	struct dentry *dentry;
461 	struct inode *inode;
462 	int err;
463 	unsigned int lookup_flags = nd->flags;
464 
465 	while (*name=='/')
466 		name++;
467 	if (!*name)
468 		goto return_reval;
469 
470 	inode = nd->dentry->d_inode;
471 	if (current->link_count)
472 		lookup_flags = LOOKUP_FOLLOW;
473 
474 	/* At this point we know we have a real path component. */
475 	for(;;) {
476 		unsigned long hash;
477 		struct qstr this;
478 		unsigned int c;
479 
480 		err = permission(inode, MAY_EXEC);
481 		dentry = ERR_PTR(err);
482  		if (err)
483 			break;
484 
485 		this.name = name;
486 		c = *(const unsigned char *)name;
487 
488 		hash = init_name_hash();
489 		do {
490 			name++;
491 			hash = partial_name_hash(c, hash);
492 			c = *(const unsigned char *)name;
493 		} while (c && (c != '/'));
494 		this.len = name - (const char *) this.name;
495 		this.hash = end_name_hash(hash);
496 
497 		/* remove trailing slashes? */
498 		if (!c)
499 			goto last_component;
500 		while (*++name == '/');
501 		if (!*name)
502 			goto last_with_slashes;
503 
504 		/*
505 		 * "." and ".." are special - ".." especially so because it has
506 		 * to be able to know about the current root directory and
507 		 * parent relationships.
508 		 */
509 		if (this.name[0] == '.') switch (this.len) {
510 			default:
511 				break;
512 			case 2:
513 				if (this.name[1] != '.')
514 					break;
515 				follow_dotdot(nd);
516 				inode = nd->dentry->d_inode;
517 				/* fallthrough */
518 			case 1:
519 				continue;
520 		}
521 		/*
522 		 * See if the low-level filesystem might want
523 		 * to use its own hash..
524 		 */
525 		if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
526 			err = nd->dentry->d_op->d_hash(nd->dentry, &this);
527 			if (err < 0)
528 				break;
529 		}
530 		/* This does the actual lookups.. */
531 		dentry = cached_lookup(nd->dentry, &this, LOOKUP_CONTINUE);
532 		if (!dentry) {
533 			dentry = real_lookup(nd->dentry, &this, LOOKUP_CONTINUE);
534 			err = PTR_ERR(dentry);
535 			if (IS_ERR(dentry))
536 				break;
537 		}
538 		/* Check mountpoints.. */
539 		while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
540 			;
541 
542 		err = -ENOENT;
543 		inode = dentry->d_inode;
544 		if (!inode)
545 			goto out_dput;
546 		err = -ENOTDIR;
547 		if (!inode->i_op)
548 			goto out_dput;
549 
550 		if (inode->i_op->follow_link) {
551 			struct vfsmount *mnt = mntget(nd->mnt);
552 			err = do_follow_link(dentry, nd);
553 			dput(dentry);
554 			mntput(mnt);
555 			if (err)
556 				goto return_err;
557 			err = -ENOENT;
558 			inode = nd->dentry->d_inode;
559 			if (!inode)
560 				break;
561 			err = -ENOTDIR;
562 			if (!inode->i_op)
563 				break;
564 		} else {
565 			dput(nd->dentry);
566 			nd->dentry = dentry;
567 		}
568 		err = -ENOTDIR;
569 		if (!inode->i_op->lookup)
570 			break;
571 		continue;
572 		/* here ends the main loop */
573 
574 last_with_slashes:
575 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
576 last_component:
577 		if (lookup_flags & LOOKUP_PARENT)
578 			goto lookup_parent;
579 		if (this.name[0] == '.') switch (this.len) {
580 			default:
581 				break;
582 			case 2:
583 				if (this.name[1] != '.')
584 					break;
585 				follow_dotdot(nd);
586 				inode = nd->dentry->d_inode;
587 				/* fallthrough */
588 			case 1:
589 				goto return_reval;
590 		}
591 		if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
592 			err = nd->dentry->d_op->d_hash(nd->dentry, &this);
593 			if (err < 0)
594 				break;
595 		}
596 		dentry = cached_lookup(nd->dentry, &this, nd->flags);
597 		if (!dentry) {
598 			dentry = real_lookup(nd->dentry, &this, nd->flags);
599 			err = PTR_ERR(dentry);
600 			if (IS_ERR(dentry))
601 				break;
602 		}
603 		while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))
604 			;
605 		inode = dentry->d_inode;
606 		if ((lookup_flags & LOOKUP_FOLLOW)
607 		    && inode && inode->i_op && inode->i_op->follow_link) {
608 			struct vfsmount *mnt = mntget(nd->mnt);
609 			err = do_follow_link(dentry, nd);
610 			dput(dentry);
611 			mntput(mnt);
612 			if (err)
613 				goto return_err;
614 			inode = nd->dentry->d_inode;
615 		} else {
616 			dput(nd->dentry);
617 			nd->dentry = dentry;
618 		}
619 		err = -ENOENT;
620 		if (!inode)
621 			goto no_inode;
622 		if (lookup_flags & LOOKUP_DIRECTORY) {
623 			err = -ENOTDIR;
624 			if (!inode->i_op || !inode->i_op->lookup)
625 				break;
626 		}
627 		goto return_base;
628 no_inode:
629 		err = -ENOENT;
630 		if (lookup_flags & (LOOKUP_POSITIVE|LOOKUP_DIRECTORY))
631 			break;
632 		goto return_base;
633 lookup_parent:
634 		nd->last = this;
635 		nd->last_type = LAST_NORM;
636 		if (this.name[0] != '.')
637 			goto return_base;
638 		if (this.len == 1)
639 			nd->last_type = LAST_DOT;
640 		else if (this.len == 2 && this.name[1] == '.')
641 			nd->last_type = LAST_DOTDOT;
642 		else
643 			goto return_base;
644 return_reval:
645 		/*
646 		 * We bypassed the ordinary revalidation routines.
647 		 * Check the cached dentry for staleness.
648 		 */
649 		dentry = nd->dentry;
650 		if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
651 			err = -ESTALE;
652 			if (!dentry->d_op->d_revalidate(dentry, 0)) {
653 				d_invalidate(dentry);
654 				break;
655 			}
656 		}
657 return_base:
658 		return 0;
659 out_dput:
660 		dput(dentry);
661 		break;
662 	}
663 	path_release(nd);
664 return_err:
665 	return err;
666 }
667 
path_walk(const char * name,struct nameidata * nd)668 int fastcall path_walk(const char * name, struct nameidata *nd)
669 {
670 	current->total_link_count = 0;
671 	return link_path_walk(name, nd);
672 }
673 
674 /* SMP-safe */
675 /* returns 1 if everything is done */
__emul_lookup_dentry(const char * name,struct nameidata * nd)676 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
677 {
678 	if (path_walk(name, nd))
679 		return 0;		/* something went wrong... */
680 
681 	if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
682 		struct nameidata nd_root;
683 		/*
684 		 * NAME was not found in alternate root or it's a directory.  Try to find
685 		 * it in the normal root:
686 		 */
687 		nd_root.last_type = LAST_ROOT;
688 		nd_root.flags = nd->flags;
689 		read_lock(&current->fs->lock);
690 		nd_root.mnt = mntget(current->fs->rootmnt);
691 		nd_root.dentry = dget(current->fs->root);
692 		read_unlock(&current->fs->lock);
693 		if (path_walk(name, &nd_root))
694 			return 1;
695 		if (nd_root.dentry->d_inode) {
696 			path_release(nd);
697 			nd->dentry = nd_root.dentry;
698 			nd->mnt = nd_root.mnt;
699 			nd->last = nd_root.last;
700 			return 1;
701 		}
702 		path_release(&nd_root);
703 	}
704 	return 1;
705 }
706 
set_fs_altroot(void)707 void set_fs_altroot(void)
708 {
709 	char *emul = __emul_prefix();
710 	struct nameidata nd;
711 	struct vfsmount *mnt = NULL, *oldmnt;
712 	struct dentry *dentry = NULL, *olddentry;
713 	if (emul) {
714 		read_lock(&current->fs->lock);
715 		nd.mnt = mntget(current->fs->rootmnt);
716 		nd.dentry = dget(current->fs->root);
717 		read_unlock(&current->fs->lock);
718 		nd.flags = LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_POSITIVE;
719 		if (path_walk(emul,&nd) == 0) {
720 			mnt = nd.mnt;
721 			dentry = nd.dentry;
722 		}
723 	}
724 	write_lock(&current->fs->lock);
725 	oldmnt = current->fs->altrootmnt;
726 	olddentry = current->fs->altroot;
727 	current->fs->altrootmnt = mnt;
728 	current->fs->altroot = dentry;
729 	write_unlock(&current->fs->lock);
730 	if (olddentry) {
731 		dput(olddentry);
732 		mntput(oldmnt);
733 	}
734 }
735 
736 /* SMP-safe */
737 static inline int
walk_init_root(const char * name,struct nameidata * nd)738 walk_init_root(const char *name, struct nameidata *nd)
739 {
740 	read_lock(&current->fs->lock);
741 	if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
742 		nd->mnt = mntget(current->fs->altrootmnt);
743 		nd->dentry = dget(current->fs->altroot);
744 		read_unlock(&current->fs->lock);
745 		if (__emul_lookup_dentry(name,nd))
746 			return 0;
747 		read_lock(&current->fs->lock);
748 	}
749 	nd->mnt = mntget(current->fs->rootmnt);
750 	nd->dentry = dget(current->fs->root);
751 	read_unlock(&current->fs->lock);
752 	return 1;
753 }
754 
755 /* SMP-safe */
path_lookup(const char * path,unsigned flags,struct nameidata * nd)756 int fastcall path_lookup(const char *path, unsigned flags, struct nameidata *nd)
757 {
758 	int error = 0;
759 	if (path_init(path, flags, nd))
760 		error = path_walk(path, nd);
761 	return error;
762 }
763 
764 
765 /* SMP-safe */
path_init(const char * name,unsigned int flags,struct nameidata * nd)766 int fastcall path_init(const char *name, unsigned int flags, struct nameidata *nd)
767 {
768 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
769 	nd->flags = flags;
770 	if (*name=='/')
771 		return walk_init_root(name,nd);
772 	read_lock(&current->fs->lock);
773 	nd->mnt = mntget(current->fs->pwdmnt);
774 	nd->dentry = dget(current->fs->pwd);
775 	read_unlock(&current->fs->lock);
776 	return 1;
777 }
778 
779 /*
780  * Restricted form of lookup. Doesn't follow links, single-component only,
781  * needs parent already locked. Doesn't follow mounts.
782  * SMP-safe.
783  */
lookup_hash(struct qstr * name,struct dentry * base)784 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
785 {
786 	struct dentry * dentry;
787 	struct inode *inode;
788 	int err;
789 
790 	inode = base->d_inode;
791 	err = permission(inode, MAY_EXEC);
792 	dentry = ERR_PTR(err);
793 	if (err)
794 		goto out;
795 
796 	/*
797 	 * See if the low-level filesystem might want
798 	 * to use its own hash..
799 	 */
800 	if (base->d_op && base->d_op->d_hash) {
801 		err = base->d_op->d_hash(base, name);
802 		dentry = ERR_PTR(err);
803 		if (err < 0)
804 			goto out;
805 	}
806 
807 	dentry = cached_lookup(base, name, 0);
808 	if (!dentry) {
809 		struct dentry *new;
810 
811 		/* Don't create child dentry for a dead directory. */
812 		dentry = ERR_PTR(-ENOENT);
813 		if (IS_DEADDIR(inode))
814 			goto out;
815 
816 		new = d_alloc(base, name);
817 		dentry = ERR_PTR(-ENOMEM);
818 		if (!new)
819 			goto out;
820 		lock_kernel();
821 		dentry = inode->i_op->lookup(inode, new);
822 		unlock_kernel();
823 		if (!dentry)
824 			dentry = new;
825 		else
826 			dput(new);
827 	}
828 out:
829 	return dentry;
830 }
831 
832 /* SMP-safe */
lookup_one_len(const char * name,struct dentry * base,int len)833 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
834 {
835 	unsigned long hash;
836 	struct qstr this;
837 	unsigned int c;
838 
839 	this.name = name;
840 	this.len = len;
841 	if (!len)
842 		goto access;
843 
844 	hash = init_name_hash();
845 	while (len--) {
846 		c = *(const unsigned char *)name++;
847 		if (c == '/' || c == '\0')
848 			goto access;
849 		hash = partial_name_hash(c, hash);
850 	}
851 	this.hash = end_name_hash(hash);
852 
853 	return lookup_hash(&this, base);
854 access:
855 	return ERR_PTR(-EACCES);
856 }
857 
858 /*
859  *	namei()
860  *
861  * is used by most simple commands to get the inode of a specified name.
862  * Open, link etc use their own routines, but this is enough for things
863  * like 'chmod' etc.
864  *
865  * namei exists in two versions: namei/lnamei. The only difference is
866  * that namei follows links, while lnamei does not.
867  * SMP-safe
868  */
__user_walk(const char * name,unsigned flags,struct nameidata * nd)869 int fastcall __user_walk(const char *name, unsigned flags, struct nameidata *nd)
870 {
871 	char *tmp;
872 	int err;
873 
874 	tmp = getname(name);
875 	err = PTR_ERR(tmp);
876 	if (!IS_ERR(tmp)) {
877 		err = 0;
878 		err = path_lookup(tmp, flags, nd);
879 		putname(tmp);
880 	}
881 	return err;
882 }
883 
884 /*
885  * It's inline, so penalty for filesystems that don't use sticky bit is
886  * minimal.
887  */
check_sticky(struct inode * dir,struct inode * inode)888 static inline int check_sticky(struct inode *dir, struct inode *inode)
889 {
890 	if (!(dir->i_mode & S_ISVTX))
891 		return 0;
892 	if (inode->i_uid == current->fsuid)
893 		return 0;
894 	if (dir->i_uid == current->fsuid)
895 		return 0;
896 	return !capable(CAP_FOWNER);
897 }
898 
899 /*
900  *	Check whether we can remove a link victim from directory dir, check
901  *  whether the type of victim is right.
902  *  1. We can't do it if dir is read-only (done in permission())
903  *  2. We should have write and exec permissions on dir
904  *  3. We can't remove anything from append-only dir
905  *  4. We can't do anything with immutable dir (done in permission())
906  *  5. If the sticky bit on dir is set we should either
907  *	a. be owner of dir, or
908  *	b. be owner of victim, or
909  *	c. have CAP_FOWNER capability
910  *  6. If the victim is append-only or immutable we can't do antyhing with
911  *     links pointing to it.
912  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
913  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
914  *  9. We can't remove a root or mountpoint.
915  */
may_delete(struct inode * dir,struct dentry * victim,int isdir)916 static inline int may_delete(struct inode *dir,struct dentry *victim, int isdir)
917 {
918 	int error;
919 	if (!victim->d_inode || victim->d_parent->d_inode != dir)
920 		return -ENOENT;
921 	error = permission(dir,MAY_WRITE | MAY_EXEC);
922 	if (error)
923 		return error;
924 	if (IS_APPEND(dir))
925 		return -EPERM;
926 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
927 	    IS_IMMUTABLE(victim->d_inode))
928 		return -EPERM;
929 	if (isdir) {
930 		if (!S_ISDIR(victim->d_inode->i_mode))
931 			return -ENOTDIR;
932 		if (IS_ROOT(victim))
933 			return -EBUSY;
934 	} else if (S_ISDIR(victim->d_inode->i_mode))
935 		return -EISDIR;
936 	if (IS_DEADDIR(dir))
937 		return -ENOENT;
938 	return 0;
939 }
940 
941 /*	Check whether we can create an object with dentry child in directory
942  *  dir.
943  *  1. We can't do it if child already exists (open has special treatment for
944  *     this case, but since we are inlined it's OK)
945  *  2. We can't do it if dir is read-only (done in permission())
946  *  3. We should have write and exec permissions on dir
947  *  4. We can't do it if dir is immutable (done in permission())
948  */
may_create(struct inode * dir,struct dentry * child)949 static inline int may_create(struct inode *dir, struct dentry *child) {
950 	if (child->d_inode)
951 		return -EEXIST;
952 	if (IS_DEADDIR(dir))
953 		return -ENOENT;
954 	return permission(dir,MAY_WRITE | MAY_EXEC);
955 }
956 
957 /*
958  * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
959  * reasons.
960  *
961  * O_DIRECTORY translates into forcing a directory lookup.
962  */
lookup_flags(unsigned int f)963 static inline int lookup_flags(unsigned int f)
964 {
965 	unsigned long retval = LOOKUP_FOLLOW;
966 
967 	if (f & O_NOFOLLOW)
968 		retval &= ~LOOKUP_FOLLOW;
969 
970 	if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
971 		retval &= ~LOOKUP_FOLLOW;
972 
973 	if (f & O_DIRECTORY)
974 		retval |= LOOKUP_DIRECTORY;
975 
976 	return retval;
977 }
978 
vfs_create(struct inode * dir,struct dentry * dentry,int mode)979 int vfs_create(struct inode *dir, struct dentry *dentry, int mode)
980 {
981 	int error;
982 
983 	mode &= S_IALLUGO;
984 	mode |= S_IFREG;
985 
986 	down(&dir->i_zombie);
987 	error = may_create(dir, dentry);
988 	if (error)
989 		goto exit_lock;
990 
991 	error = -EACCES;	/* shouldn't it be ENOSYS? */
992 	if (!dir->i_op || !dir->i_op->create)
993 		goto exit_lock;
994 
995 	DQUOT_INIT(dir);
996 	lock_kernel();
997 	error = dir->i_op->create(dir, dentry, mode);
998 	unlock_kernel();
999 exit_lock:
1000 	up(&dir->i_zombie);
1001 	if (!error)
1002 		inode_dir_notify(dir, DN_CREATE);
1003 	return error;
1004 }
1005 
1006 /*
1007  *	open_namei()
1008  *
1009  * namei for open - this is in fact almost the whole open-routine.
1010  *
1011  * Note that the low bits of "flag" aren't the same as in the open
1012  * system call - they are 00 - no permissions needed
1013  *			  01 - read permission needed
1014  *			  10 - write permission needed
1015  *			  11 - read/write permissions needed
1016  * which is a lot more logical, and also allows the "no perm" needed
1017  * for symlinks (where the permissions are checked later).
1018  * SMP-safe
1019  */
open_namei(const char * pathname,int flag,int mode,struct nameidata * nd)1020 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1021 {
1022 	int acc_mode, error = 0;
1023 	struct inode *inode;
1024 	struct dentry *dentry;
1025 	struct vfsmount *mnt;
1026 	struct dentry *dir;
1027 	int count = 0;
1028 
1029 	acc_mode = ACC_MODE(flag);
1030 
1031 	/*
1032 	 * The simplest case - just a plain lookup.
1033 	 */
1034 	if (!(flag & O_CREAT)) {
1035 		error = path_lookup(pathname, lookup_flags(flag), nd);
1036 		if (error)
1037 			return error;
1038 		dentry = nd->dentry;
1039 		goto ok;
1040 	}
1041 
1042 	/*
1043 	 * Create - we need to know the parent.
1044 	 */
1045 	error = path_lookup(pathname, LOOKUP_PARENT, nd);
1046 	if (error)
1047 		return error;
1048 
1049 	/*
1050 	 * We have the parent and last component. First of all, check
1051 	 * that we are not asked to creat(2) an obvious directory - that
1052 	 * will not do.
1053 	 */
1054 	error = -EISDIR;
1055 	if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1056 		goto exit;
1057 
1058 	dir = nd->dentry;
1059 	down(&dir->d_inode->i_sem);
1060 	dentry = lookup_hash(&nd->last, nd->dentry);
1061 
1062 do_last:
1063 	error = PTR_ERR(dentry);
1064 	if (IS_ERR(dentry)) {
1065 		up(&dir->d_inode->i_sem);
1066 		goto exit;
1067 	}
1068 
1069 	/* Negative dentry, just create the file */
1070 	if (!dentry->d_inode) {
1071 		error = vfs_create(dir->d_inode, dentry,
1072 				   mode & ~current->fs->umask);
1073 		up(&dir->d_inode->i_sem);
1074 		dput(nd->dentry);
1075 		nd->dentry = dentry;
1076 		if (error)
1077 			goto exit;
1078 		/* Don't check for write permission, don't truncate */
1079 		acc_mode = 0;
1080 		flag &= ~O_TRUNC;
1081 		goto ok;
1082 	}
1083 
1084 	/*
1085 	 * It already exists.
1086 	 */
1087 	up(&dir->d_inode->i_sem);
1088 
1089 	error = -EEXIST;
1090 	if (flag & O_EXCL)
1091 		goto exit_dput;
1092 
1093 	if (d_mountpoint(dentry)) {
1094 		error = -ELOOP;
1095 		if (flag & O_NOFOLLOW)
1096 			goto exit_dput;
1097 		while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1098 	}
1099 	error = -ENOENT;
1100 	if (!dentry->d_inode)
1101 		goto exit_dput;
1102 	if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1103 		goto do_link;
1104 
1105 	dput(nd->dentry);
1106 	nd->dentry = dentry;
1107 	error = -EISDIR;
1108 	if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1109 		goto exit;
1110 ok:
1111 	error = -ENOENT;
1112 	inode = dentry->d_inode;
1113 	if (!inode)
1114 		goto exit;
1115 
1116 	error = -ELOOP;
1117 	if (S_ISLNK(inode->i_mode))
1118 		goto exit;
1119 
1120 	error = -EISDIR;
1121 	if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1122 		goto exit;
1123 
1124 	error = permission(inode,acc_mode);
1125 	if (error)
1126 		goto exit;
1127 
1128 	/*
1129 	 * FIFO's, sockets and device files are special: they don't
1130 	 * actually live on the filesystem itself, and as such you
1131 	 * can write to them even if the filesystem is read-only.
1132 	 */
1133 	if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1134 	    	flag &= ~O_TRUNC;
1135 	} else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1136 		error = -EACCES;
1137 		if (nd->mnt->mnt_flags & MNT_NODEV)
1138 			goto exit;
1139 
1140 		flag &= ~O_TRUNC;
1141 	} else {
1142 		error = -EROFS;
1143 		if (IS_RDONLY(inode) && (flag & 2))
1144 			goto exit;
1145 	}
1146 	/*
1147 	 * An append-only file must be opened in append mode for writing.
1148 	 */
1149 	error = -EPERM;
1150 	if (IS_APPEND(inode)) {
1151 		if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1152 			goto exit;
1153 		if (flag & O_TRUNC)
1154 			goto exit;
1155 	}
1156 
1157 	/*
1158 	 * Ensure there are no outstanding leases on the file.
1159 	 */
1160 	error = get_lease(inode, flag);
1161 	if (error)
1162 		goto exit;
1163 
1164 	if (flag & O_TRUNC) {
1165 		error = get_write_access(inode);
1166 		if (error)
1167 			goto exit;
1168 
1169 		/*
1170 		 * Refuse to truncate files with mandatory locks held on them.
1171 		 */
1172 		error = locks_verify_locked(inode);
1173 		if (!error) {
1174 			DQUOT_INIT(inode);
1175 
1176 			error = do_truncate(dentry, 0);
1177 		}
1178 		put_write_access(inode);
1179 		if (error)
1180 			goto exit;
1181 	} else
1182 		if (flag & FMODE_WRITE)
1183 			DQUOT_INIT(inode);
1184 
1185 	return 0;
1186 
1187 exit_dput:
1188 	dput(dentry);
1189 exit:
1190 	path_release(nd);
1191 	return error;
1192 
1193 do_link:
1194 	error = -ELOOP;
1195 	if (flag & O_NOFOLLOW)
1196 		goto exit_dput;
1197 	/*
1198 	 * This is subtle. Instead of calling do_follow_link() we do the
1199 	 * thing by hands. The reason is that this way we have zero link_count
1200 	 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1201 	 * After that we have the parent and last component, i.e.
1202 	 * we are in the same situation as after the first path_walk().
1203 	 * Well, almost - if the last component is normal we get its copy
1204 	 * stored in nd->last.name and we will have to putname() it when we
1205 	 * are done. Procfs-like symlinks just set LAST_BIND.
1206 	 */
1207 	UPDATE_ATIME(dentry->d_inode);
1208 	mnt = mntget(nd->mnt);
1209 	error = dentry->d_inode->i_op->follow_link(dentry, nd);
1210 	dput(dentry);
1211 	mntput(mnt);
1212 	if (error)
1213 		return error;
1214 	if (nd->last_type == LAST_BIND) {
1215 		dentry = nd->dentry;
1216 		goto ok;
1217 	}
1218 	error = -EISDIR;
1219 	if (nd->last_type != LAST_NORM)
1220 		goto exit;
1221 	if (nd->last.name[nd->last.len]) {
1222 		putname(nd->last.name);
1223 		goto exit;
1224 	}
1225 	error = -ELOOP;
1226 	if (count++==32) {
1227 		putname(nd->last.name);
1228 		goto exit;
1229 	}
1230 	dir = nd->dentry;
1231 	down(&dir->d_inode->i_sem);
1232 	dentry = lookup_hash(&nd->last, nd->dentry);
1233 	putname(nd->last.name);
1234 	goto do_last;
1235 }
1236 
1237 /* SMP-safe */
lookup_create(struct nameidata * nd,int is_dir)1238 static struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1239 {
1240 	struct dentry *dentry;
1241 
1242 	down(&nd->dentry->d_inode->i_sem);
1243 	dentry = ERR_PTR(-EEXIST);
1244 	if (nd->last_type != LAST_NORM)
1245 		goto fail;
1246 	dentry = lookup_hash(&nd->last, nd->dentry);
1247 	if (IS_ERR(dentry))
1248 		goto fail;
1249 	if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1250 		goto enoent;
1251 	return dentry;
1252 enoent:
1253 	dput(dentry);
1254 	dentry = ERR_PTR(-ENOENT);
1255 fail:
1256 	return dentry;
1257 }
1258 
vfs_mknod(struct inode * dir,struct dentry * dentry,int mode,dev_t dev)1259 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1260 {
1261 	int error = -EPERM;
1262 
1263 	down(&dir->i_zombie);
1264 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1265 		goto exit_lock;
1266 
1267 	error = may_create(dir, dentry);
1268 	if (error)
1269 		goto exit_lock;
1270 
1271 	error = -EPERM;
1272 	if (!dir->i_op || !dir->i_op->mknod)
1273 		goto exit_lock;
1274 
1275 	DQUOT_INIT(dir);
1276 	lock_kernel();
1277 	error = dir->i_op->mknod(dir, dentry, mode, dev);
1278 	unlock_kernel();
1279 exit_lock:
1280 	up(&dir->i_zombie);
1281 	if (!error)
1282 		inode_dir_notify(dir, DN_CREATE);
1283 	return error;
1284 }
1285 
sys_mknod(const char * filename,int mode,dev_t dev)1286 asmlinkage long sys_mknod(const char * filename, int mode, dev_t dev)
1287 {
1288 	int error = 0;
1289 	char * tmp;
1290 	struct dentry * dentry;
1291 	struct nameidata nd;
1292 
1293 	if (S_ISDIR(mode))
1294 		return -EPERM;
1295 	tmp = getname(filename);
1296 	if (IS_ERR(tmp))
1297 		return PTR_ERR(tmp);
1298 
1299 	error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1300 	if (error)
1301 		goto out;
1302 	dentry = lookup_create(&nd, 0);
1303 	error = PTR_ERR(dentry);
1304 
1305 	mode &= ~current->fs->umask;
1306 	if (!IS_ERR(dentry)) {
1307 		switch (mode & S_IFMT) {
1308 		case 0: case S_IFREG:
1309 			error = vfs_create(nd.dentry->d_inode,dentry,mode);
1310 			break;
1311 		case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
1312 			error = vfs_mknod(nd.dentry->d_inode,dentry,mode,dev);
1313 			break;
1314 		case S_IFDIR:
1315 			error = -EPERM;
1316 			break;
1317 		default:
1318 			error = -EINVAL;
1319 		}
1320 		dput(dentry);
1321 	}
1322 	up(&nd.dentry->d_inode->i_sem);
1323 	path_release(&nd);
1324 out:
1325 	putname(tmp);
1326 
1327 	return error;
1328 }
1329 
vfs_mkdir(struct inode * dir,struct dentry * dentry,int mode)1330 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1331 {
1332 	int error;
1333 
1334 	down(&dir->i_zombie);
1335 	error = may_create(dir, dentry);
1336 	if (error)
1337 		goto exit_lock;
1338 
1339 	error = -EPERM;
1340 	if (!dir->i_op || !dir->i_op->mkdir)
1341 		goto exit_lock;
1342 
1343 	DQUOT_INIT(dir);
1344 	mode &= (S_IRWXUGO|S_ISVTX);
1345 	lock_kernel();
1346 	error = dir->i_op->mkdir(dir, dentry, mode);
1347 	unlock_kernel();
1348 
1349 exit_lock:
1350 	up(&dir->i_zombie);
1351 	if (!error)
1352 		inode_dir_notify(dir, DN_CREATE);
1353 	return error;
1354 }
1355 
sys_mkdir(const char * pathname,int mode)1356 asmlinkage long sys_mkdir(const char * pathname, int mode)
1357 {
1358 	int error = 0;
1359 	char * tmp;
1360 
1361 	tmp = getname(pathname);
1362 	error = PTR_ERR(tmp);
1363 	if (!IS_ERR(tmp)) {
1364 		struct dentry *dentry;
1365 		struct nameidata nd;
1366 
1367 		error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1368 		if (error)
1369 			goto out;
1370 		dentry = lookup_create(&nd, 1);
1371 		error = PTR_ERR(dentry);
1372 		if (!IS_ERR(dentry)) {
1373 			error = vfs_mkdir(nd.dentry->d_inode, dentry,
1374 					  mode & ~current->fs->umask);
1375 			dput(dentry);
1376 		}
1377 		up(&nd.dentry->d_inode->i_sem);
1378 		path_release(&nd);
1379 out:
1380 		putname(tmp);
1381 	}
1382 
1383 	return error;
1384 }
1385 
1386 /*
1387  * We try to drop the dentry early: we should have
1388  * a usage count of 2 if we're the only user of this
1389  * dentry, and if that is true (possibly after pruning
1390  * the dcache), then we drop the dentry now.
1391  *
1392  * A low-level filesystem can, if it choses, legally
1393  * do a
1394  *
1395  *	if (!d_unhashed(dentry))
1396  *		return -EBUSY;
1397  *
1398  * if it cannot handle the case of removing a directory
1399  * that is still in use by something else..
1400  */
d_unhash(struct dentry * dentry)1401 static void d_unhash(struct dentry *dentry)
1402 {
1403 	dget(dentry);
1404 	spin_lock(&dcache_lock);
1405 	switch (atomic_read(&dentry->d_count)) {
1406 	default:
1407 		spin_unlock(&dcache_lock);
1408 		shrink_dcache_parent(dentry);
1409 		spin_lock(&dcache_lock);
1410 		if (atomic_read(&dentry->d_count) != 2)
1411 			break;
1412 	case 2:
1413 		list_del_init(&dentry->d_hash);
1414 	}
1415 	spin_unlock(&dcache_lock);
1416 }
1417 
vfs_rmdir(struct inode * dir,struct dentry * dentry)1418 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1419 {
1420 	int error;
1421 
1422 	error = may_delete(dir, dentry, 1);
1423 	if (error)
1424 		return error;
1425 
1426 	if (!dir->i_op || !dir->i_op->rmdir)
1427 		return -EPERM;
1428 
1429 	DQUOT_INIT(dir);
1430 
1431 	double_down(&dir->i_zombie, &dentry->d_inode->i_zombie);
1432 	d_unhash(dentry);
1433 	if (d_mountpoint(dentry))
1434 		error = -EBUSY;
1435 	else {
1436 		lock_kernel();
1437 		error = dir->i_op->rmdir(dir, dentry);
1438 		unlock_kernel();
1439 		if (!error)
1440 			dentry->d_inode->i_flags |= S_DEAD;
1441 	}
1442 	double_up(&dir->i_zombie, &dentry->d_inode->i_zombie);
1443 	if (!error) {
1444 		inode_dir_notify(dir, DN_DELETE);
1445 		d_delete(dentry);
1446 	}
1447 	dput(dentry);
1448 
1449 	return error;
1450 }
1451 
sys_rmdir(const char * pathname)1452 asmlinkage long sys_rmdir(const char * pathname)
1453 {
1454 	int error = 0;
1455 	char * name;
1456 	struct dentry *dentry;
1457 	struct nameidata nd;
1458 
1459 	name = getname(pathname);
1460 	if(IS_ERR(name))
1461 		return PTR_ERR(name);
1462 
1463 	error = path_lookup(name, LOOKUP_PARENT, &nd);
1464 	if (error)
1465 		goto exit;
1466 
1467 	switch(nd.last_type) {
1468 		case LAST_DOTDOT:
1469 			error = -ENOTEMPTY;
1470 			goto exit1;
1471 		case LAST_DOT:
1472 			error = -EINVAL;
1473 			goto exit1;
1474 		case LAST_ROOT:
1475 			error = -EBUSY;
1476 			goto exit1;
1477 	}
1478 	down(&nd.dentry->d_inode->i_sem);
1479 	dentry = lookup_hash(&nd.last, nd.dentry);
1480 	error = PTR_ERR(dentry);
1481 	if (!IS_ERR(dentry)) {
1482 		error = vfs_rmdir(nd.dentry->d_inode, dentry);
1483 		dput(dentry);
1484 	}
1485 	up(&nd.dentry->d_inode->i_sem);
1486 exit1:
1487 	path_release(&nd);
1488 exit:
1489 	putname(name);
1490 	return error;
1491 }
1492 
vfs_unlink(struct inode * dir,struct dentry * dentry)1493 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1494 {
1495 	int error;
1496 	struct inode *inode;
1497 
1498 	error = may_delete(dir, dentry, 0);
1499 	if (error)
1500 		return error;
1501 
1502 	inode = dentry->d_inode;
1503 	atomic_inc(&inode->i_count);
1504 	double_down(&dir->i_zombie, &inode->i_zombie);
1505 
1506 	error = -EPERM;
1507 	if (dir->i_op && dir->i_op->unlink) {
1508 		DQUOT_INIT(dir);
1509 		if (d_mountpoint(dentry))
1510 			error = -EBUSY;
1511 		else {
1512 			lock_kernel();
1513 			error = dir->i_op->unlink(dir, dentry);
1514 			unlock_kernel();
1515 		}
1516 	}
1517 	double_up(&dir->i_zombie, &inode->i_zombie);
1518 	iput(inode);
1519 
1520 	if (!error) {
1521 		d_delete(dentry);
1522 		inode_dir_notify(dir, DN_DELETE);
1523 	}
1524 	return error;
1525 }
1526 
sys_unlink(const char * pathname)1527 asmlinkage long sys_unlink(const char * pathname)
1528 {
1529 	int error = 0;
1530 	char * name;
1531 	struct dentry *dentry;
1532 	struct nameidata nd;
1533 
1534 	name = getname(pathname);
1535 	if(IS_ERR(name))
1536 		return PTR_ERR(name);
1537 
1538 	error = path_lookup(name, LOOKUP_PARENT, &nd);
1539 	if (error)
1540 		goto exit;
1541 	error = -EISDIR;
1542 	if (nd.last_type != LAST_NORM)
1543 		goto exit1;
1544 	down(&nd.dentry->d_inode->i_sem);
1545 	dentry = lookup_hash(&nd.last, nd.dentry);
1546 	error = PTR_ERR(dentry);
1547 	if (!IS_ERR(dentry)) {
1548 		/* Why not before? Because we want correct error value */
1549 		if (nd.last.name[nd.last.len])
1550 			goto slashes;
1551 		error = vfs_unlink(nd.dentry->d_inode, dentry);
1552 	exit2:
1553 		dput(dentry);
1554 	}
1555 	up(&nd.dentry->d_inode->i_sem);
1556 exit1:
1557 	path_release(&nd);
1558 exit:
1559 	putname(name);
1560 
1561 	return error;
1562 
1563 slashes:
1564 	error = !dentry->d_inode ? -ENOENT :
1565 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1566 	goto exit2;
1567 }
1568 
vfs_symlink(struct inode * dir,struct dentry * dentry,const char * oldname)1569 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1570 {
1571 	int error;
1572 
1573 	down(&dir->i_zombie);
1574 	error = may_create(dir, dentry);
1575 	if (error)
1576 		goto exit_lock;
1577 
1578 	error = -EPERM;
1579 	if (!dir->i_op || !dir->i_op->symlink)
1580 		goto exit_lock;
1581 
1582 	DQUOT_INIT(dir);
1583 	lock_kernel();
1584 	error = dir->i_op->symlink(dir, dentry, oldname);
1585 	unlock_kernel();
1586 
1587 exit_lock:
1588 	up(&dir->i_zombie);
1589 	if (!error)
1590 		inode_dir_notify(dir, DN_CREATE);
1591 	return error;
1592 }
1593 
sys_symlink(const char * oldname,const char * newname)1594 asmlinkage long sys_symlink(const char * oldname, const char * newname)
1595 {
1596 	int error = 0;
1597 	char * from;
1598 	char * to;
1599 
1600 	from = getname(oldname);
1601 	if(IS_ERR(from))
1602 		return PTR_ERR(from);
1603 	to = getname(newname);
1604 	error = PTR_ERR(to);
1605 	if (!IS_ERR(to)) {
1606 		struct dentry *dentry;
1607 		struct nameidata nd;
1608 
1609 		error = path_lookup(to, LOOKUP_PARENT, &nd);
1610 		if (error)
1611 			goto out;
1612 		dentry = lookup_create(&nd, 0);
1613 		error = PTR_ERR(dentry);
1614 		if (!IS_ERR(dentry)) {
1615 			error = vfs_symlink(nd.dentry->d_inode, dentry, from);
1616 			dput(dentry);
1617 		}
1618 		up(&nd.dentry->d_inode->i_sem);
1619 		path_release(&nd);
1620 out:
1621 		putname(to);
1622 	}
1623 	putname(from);
1624 	return error;
1625 }
1626 
vfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)1627 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1628 {
1629 	struct inode *inode;
1630 	int error;
1631 
1632 	error = -ENOENT;
1633 	inode = old_dentry->d_inode;
1634 	if (!inode)
1635 		goto exit;
1636 
1637 	error = -EXDEV;
1638 	if (dir->i_dev != inode->i_dev)
1639 		goto exit;
1640 
1641 	double_down(&dir->i_zombie, &old_dentry->d_inode->i_zombie);
1642 
1643 	error = may_create(dir, new_dentry);
1644 	if (error)
1645 		goto exit_lock;
1646 
1647 	/*
1648 	 * A link to an append-only or immutable file cannot be created.
1649 	 */
1650 	error = -EPERM;
1651 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1652 		goto exit_lock;
1653 	if (!dir->i_op || !dir->i_op->link)
1654 		goto exit_lock;
1655 
1656 	DQUOT_INIT(dir);
1657 	lock_kernel();
1658 	error = dir->i_op->link(old_dentry, dir, new_dentry);
1659 	unlock_kernel();
1660 
1661 exit_lock:
1662 	double_up(&dir->i_zombie, &old_dentry->d_inode->i_zombie);
1663 	if (!error)
1664 		inode_dir_notify(dir, DN_CREATE);
1665 exit:
1666 	return error;
1667 }
1668 
1669 /*
1670  * Hardlinks are often used in delicate situations.  We avoid
1671  * security-related surprises by not following symlinks on the
1672  * newname.  --KAB
1673  *
1674  * We don't follow them on the oldname either to be compatible
1675  * with linux 2.0, and to avoid hard-linking to directories
1676  * and other special files.  --ADM
1677  */
sys_link(const char * oldname,const char * newname)1678 asmlinkage long sys_link(const char * oldname, const char * newname)
1679 {
1680 	int error;
1681 	char * to;
1682 
1683 	to = getname(newname);
1684 	error = PTR_ERR(to);
1685 	if (!IS_ERR(to)) {
1686 		struct dentry *new_dentry;
1687 		struct nameidata nd, old_nd;
1688 
1689 		error = __user_walk(oldname, LOOKUP_POSITIVE, &old_nd);
1690 		if (error)
1691 			goto exit;
1692 		error = path_lookup(to, LOOKUP_PARENT, &nd);
1693 		if (error)
1694 			goto out;
1695 		error = -EXDEV;
1696 		if (old_nd.mnt != nd.mnt)
1697 			goto out_release;
1698 		new_dentry = lookup_create(&nd, 0);
1699 		error = PTR_ERR(new_dentry);
1700 		if (!IS_ERR(new_dentry)) {
1701 			error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1702 			dput(new_dentry);
1703 		}
1704 		up(&nd.dentry->d_inode->i_sem);
1705 out_release:
1706 		path_release(&nd);
1707 out:
1708 		path_release(&old_nd);
1709 exit:
1710 		putname(to);
1711 	}
1712 	return error;
1713 }
1714 
1715 /*
1716  * The worst of all namespace operations - renaming directory. "Perverted"
1717  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
1718  * Problems:
1719  *	a) we can get into loop creation. Check is done in is_subdir().
1720  *	b) race potential - two innocent renames can create a loop together.
1721  *	   That's where 4.4 screws up. Current fix: serialization on
1722  *	   sb->s_vfs_rename_sem. We might be more accurate, but that's another
1723  *	   story.
1724  *	c) we have to lock _three_ objects - parents and victim (if it exists).
1725  *	   And that - after we got ->i_sem on parents (until then we don't know
1726  *	   whether the target exists at all, let alone whether it is a directory
1727  *	   or not). Solution: ->i_zombie. Taken only after ->i_sem. Always taken
1728  *	   on link creation/removal of any kind. And taken (without ->i_sem) on
1729  *	   directory that will be removed (both in rmdir() and here).
1730  *	d) some filesystems don't support opened-but-unlinked directories,
1731  *	   either because of layout or because they are not ready to deal with
1732  *	   all cases correctly. The latter will be fixed (taking this sort of
1733  *	   stuff into VFS), but the former is not going away. Solution: the same
1734  *	   trick as in rmdir().
1735  *	e) conversion from fhandle to dentry may come in the wrong moment - when
1736  *	   we are removing the target. Solution: we will have to grab ->i_zombie
1737  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
1738  *	   ->i_sem on parents, which works but leads to some truely excessive
1739  *	   locking].
1740  */
vfs_rename_dir(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)1741 int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
1742 	       struct inode *new_dir, struct dentry *new_dentry)
1743 {
1744 	int error;
1745 	struct inode *target;
1746 
1747 	if (old_dentry->d_inode == new_dentry->d_inode)
1748 		return 0;
1749 
1750 	error = may_delete(old_dir, old_dentry, 1);
1751 	if (error)
1752 		return error;
1753 
1754 	if (new_dir->i_dev != old_dir->i_dev)
1755 		return -EXDEV;
1756 
1757 	if (!new_dentry->d_inode)
1758 		error = may_create(new_dir, new_dentry);
1759 	else
1760 		error = may_delete(new_dir, new_dentry, 1);
1761 	if (error)
1762 		return error;
1763 
1764 	if (!old_dir->i_op || !old_dir->i_op->rename)
1765 		return -EPERM;
1766 
1767 	/*
1768 	 * If we are going to change the parent - check write permissions,
1769 	 * we'll need to flip '..'.
1770 	 */
1771 	if (new_dir != old_dir) {
1772 		error = permission(old_dentry->d_inode, MAY_WRITE);
1773 	}
1774 	if (error)
1775 		return error;
1776 
1777 	DQUOT_INIT(old_dir);
1778 	DQUOT_INIT(new_dir);
1779 	down(&old_dir->i_sb->s_vfs_rename_sem);
1780 	error = -EINVAL;
1781 	if (is_subdir(new_dentry, old_dentry))
1782 		goto out_unlock;
1783 	/* Don't eat your daddy, dear... */
1784 	/* This also avoids locking issues */
1785 	if (old_dentry->d_parent == new_dentry)
1786 		goto out_unlock;
1787 	target = new_dentry->d_inode;
1788 	if (target) { /* Hastur! Hastur! Hastur! */
1789 		triple_down(&old_dir->i_zombie,
1790 			    &new_dir->i_zombie,
1791 			    &target->i_zombie);
1792 		d_unhash(new_dentry);
1793 	} else
1794 		double_down(&old_dir->i_zombie,
1795 			    &new_dir->i_zombie);
1796 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1797 		error = -EBUSY;
1798 	else
1799 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1800 	if (target) {
1801 		if (!error)
1802 			target->i_flags |= S_DEAD;
1803 		triple_up(&old_dir->i_zombie,
1804 			  &new_dir->i_zombie,
1805 			  &target->i_zombie);
1806 		if (d_unhashed(new_dentry))
1807 			d_rehash(new_dentry);
1808 		dput(new_dentry);
1809 	} else
1810 		double_up(&old_dir->i_zombie,
1811 			  &new_dir->i_zombie);
1812 
1813 	if (!error)
1814 		d_move(old_dentry,new_dentry);
1815 out_unlock:
1816 	up(&old_dir->i_sb->s_vfs_rename_sem);
1817 	return error;
1818 }
1819 
vfs_rename_other(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)1820 int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
1821 	       struct inode *new_dir, struct dentry *new_dentry)
1822 {
1823 	int error;
1824 
1825 	if (old_dentry->d_inode == new_dentry->d_inode)
1826 		return 0;
1827 
1828 	error = may_delete(old_dir, old_dentry, 0);
1829 	if (error)
1830 		return error;
1831 
1832 	if (new_dir->i_dev != old_dir->i_dev)
1833 		return -EXDEV;
1834 
1835 	if (!new_dentry->d_inode)
1836 		error = may_create(new_dir, new_dentry);
1837 	else
1838 		error = may_delete(new_dir, new_dentry, 0);
1839 	if (error)
1840 		return error;
1841 
1842 	if (!old_dir->i_op || !old_dir->i_op->rename)
1843 		return -EPERM;
1844 
1845 	DQUOT_INIT(old_dir);
1846 	DQUOT_INIT(new_dir);
1847 	double_down(&old_dir->i_zombie, &new_dir->i_zombie);
1848 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
1849 		error = -EBUSY;
1850 	else
1851 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
1852 	double_up(&old_dir->i_zombie, &new_dir->i_zombie);
1853 	if (error)
1854 		return error;
1855 	/* The following d_move() should become unconditional */
1856 	if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME)) {
1857 		d_move(old_dentry, new_dentry);
1858 	}
1859 	return 0;
1860 }
1861 
vfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)1862 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1863 	       struct inode *new_dir, struct dentry *new_dentry)
1864 {
1865 	int error;
1866 	if (S_ISDIR(old_dentry->d_inode->i_mode))
1867 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1868 	else
1869 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1870 	if (!error) {
1871 		if (old_dir == new_dir)
1872 			inode_dir_notify(old_dir, DN_RENAME);
1873 		else {
1874 			inode_dir_notify(old_dir, DN_DELETE);
1875 			inode_dir_notify(new_dir, DN_CREATE);
1876 		}
1877 	}
1878 	return error;
1879 }
1880 
do_rename(const char * oldname,const char * newname)1881 static inline int do_rename(const char * oldname, const char * newname)
1882 {
1883 	int error = 0;
1884 	struct dentry * old_dir, * new_dir;
1885 	struct dentry * old_dentry, *new_dentry;
1886 	struct nameidata oldnd, newnd;
1887 
1888 	error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
1889 	if (error)
1890 		goto exit;
1891 
1892 	error = path_lookup(newname, LOOKUP_PARENT, &newnd);
1893 	if (error)
1894 		goto exit1;
1895 
1896 	error = -EXDEV;
1897 	if (oldnd.mnt != newnd.mnt)
1898 		goto exit2;
1899 
1900 	old_dir = oldnd.dentry;
1901 	error = -EBUSY;
1902 	if (oldnd.last_type != LAST_NORM)
1903 		goto exit2;
1904 
1905 	new_dir = newnd.dentry;
1906 	if (newnd.last_type != LAST_NORM)
1907 		goto exit2;
1908 
1909 	double_lock(new_dir, old_dir);
1910 
1911 	old_dentry = lookup_hash(&oldnd.last, old_dir);
1912 	error = PTR_ERR(old_dentry);
1913 	if (IS_ERR(old_dentry))
1914 		goto exit3;
1915 	/* source must exist */
1916 	error = -ENOENT;
1917 	if (!old_dentry->d_inode)
1918 		goto exit4;
1919 	/* unless the source is a directory trailing slashes give -ENOTDIR */
1920 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
1921 		error = -ENOTDIR;
1922 		if (oldnd.last.name[oldnd.last.len])
1923 			goto exit4;
1924 		if (newnd.last.name[newnd.last.len])
1925 			goto exit4;
1926 	}
1927 	new_dentry = lookup_hash(&newnd.last, new_dir);
1928 	error = PTR_ERR(new_dentry);
1929 	if (IS_ERR(new_dentry))
1930 		goto exit4;
1931 
1932 	lock_kernel();
1933 	error = vfs_rename(old_dir->d_inode, old_dentry,
1934 				   new_dir->d_inode, new_dentry);
1935 	unlock_kernel();
1936 
1937 	dput(new_dentry);
1938 exit4:
1939 	dput(old_dentry);
1940 exit3:
1941 	double_up(&new_dir->d_inode->i_sem, &old_dir->d_inode->i_sem);
1942 exit2:
1943 	path_release(&newnd);
1944 exit1:
1945 	path_release(&oldnd);
1946 exit:
1947 	return error;
1948 }
1949 
sys_rename(const char * oldname,const char * newname)1950 asmlinkage long sys_rename(const char * oldname, const char * newname)
1951 {
1952 	int error;
1953 	char * from;
1954 	char * to;
1955 
1956 	from = getname(oldname);
1957 	if(IS_ERR(from))
1958 		return PTR_ERR(from);
1959 	to = getname(newname);
1960 	error = PTR_ERR(to);
1961 	if (!IS_ERR(to)) {
1962 		error = do_rename(from,to);
1963 		putname(to);
1964 	}
1965 	putname(from);
1966 	return error;
1967 }
1968 
vfs_readlink(struct dentry * dentry,char * buffer,int buflen,const char * link)1969 int vfs_readlink(struct dentry *dentry, char *buffer, int buflen, const char *link)
1970 {
1971 	int len;
1972 
1973 	len = PTR_ERR(link);
1974 	if (IS_ERR(link))
1975 		goto out;
1976 
1977 	len = strlen(link);
1978 	if (len > (unsigned) buflen)
1979 		len = buflen;
1980 	if (copy_to_user(buffer, link, len))
1981 		len = -EFAULT;
1982 out:
1983 	return len;
1984 }
1985 
1986 static inline int
__vfs_follow_link(struct nameidata * nd,const char * link)1987 __vfs_follow_link(struct nameidata *nd, const char *link)
1988 {
1989 	int res = 0;
1990 	char *name;
1991 	if (IS_ERR(link))
1992 		goto fail;
1993 
1994 	if (*link == '/') {
1995 		path_release(nd);
1996 		if (!walk_init_root(link, nd))
1997 			/* weird __emul_prefix() stuff did it */
1998 			goto out;
1999 	}
2000 	res = link_path_walk(link, nd);
2001 out:
2002 	if (current->link_count || res || nd->last_type!=LAST_NORM)
2003 		return res;
2004 	/*
2005 	 * If it is an iterative symlinks resolution in open_namei() we
2006 	 * have to copy the last component. And all that crap because of
2007 	 * bloody create() on broken symlinks. Furrfu...
2008 	 */
2009 	name = __getname();
2010 	if (!name) {
2011 		path_release(nd);
2012 		return -ENOMEM;
2013 	}
2014 	strcpy(name, nd->last.name);
2015 	nd->last.name = name;
2016 	return 0;
2017 fail:
2018 	path_release(nd);
2019 	return PTR_ERR(link);
2020 }
2021 
vfs_follow_link(struct nameidata * nd,const char * link)2022 int vfs_follow_link(struct nameidata *nd, const char *link)
2023 {
2024 	return __vfs_follow_link(nd, link);
2025 }
2026 
2027 /* get the link contents into pagecache */
page_getlink(struct dentry * dentry,struct page ** ppage)2028 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2029 {
2030 	struct page * page;
2031 	struct address_space *mapping = dentry->d_inode->i_mapping;
2032 	page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2033 				NULL);
2034 	if (IS_ERR(page))
2035 		goto sync_fail;
2036 	wait_on_page(page);
2037 	if (!Page_Uptodate(page))
2038 		goto async_fail;
2039 	*ppage = page;
2040 	return kmap(page);
2041 
2042 async_fail:
2043 	page_cache_release(page);
2044 	return ERR_PTR(-EIO);
2045 
2046 sync_fail:
2047 	return (char*)page;
2048 }
2049 
page_readlink(struct dentry * dentry,char * buffer,int buflen)2050 int page_readlink(struct dentry *dentry, char *buffer, int buflen)
2051 {
2052 	struct page *page = NULL;
2053 	char *s = page_getlink(dentry, &page);
2054 	int res = vfs_readlink(dentry,buffer,buflen,s);
2055 	if (page) {
2056 		kunmap(page);
2057 		page_cache_release(page);
2058 	}
2059 	return res;
2060 }
2061 
page_follow_link(struct dentry * dentry,struct nameidata * nd)2062 int page_follow_link(struct dentry *dentry, struct nameidata *nd)
2063 {
2064 	struct page *page = NULL;
2065 	char *s = page_getlink(dentry, &page);
2066 	int res = __vfs_follow_link(nd, s);
2067 	if (page) {
2068 		kunmap(page);
2069 		page_cache_release(page);
2070 	}
2071 	return res;
2072 }
2073 
2074 struct inode_operations page_symlink_inode_operations = {
2075 	readlink:	page_readlink,
2076 	follow_link:	page_follow_link,
2077 };
2078