root.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 823 行 · 第 1/2 页

C
823
字号
/* -*- c -*- --------------------------------------------------------------- * * * linux/fs/autofs/root.c * *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved *  Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org> *  Copyright 2001-2003 Ian Kent <raven@themaw.net> * * This file is part of the Linux kernel and is made available under * the terms of the GNU General Public License, version 2, or at your * option, any later version, incorporated herein by reference. * * ------------------------------------------------------------------------- */#include <linux/errno.h>#include <linux/stat.h>#include <linux/param.h>#include <linux/time.h>#include <linux/smp_lock.h>#include "autofs_i.h"static struct dentry *autofs4_dir_lookup(struct inode *,struct dentry *, struct nameidata *);static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);static int autofs4_dir_unlink(struct inode *,struct dentry *);static int autofs4_dir_rmdir(struct inode *,struct dentry *);static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);static int autofs4_dir_open(struct inode *inode, struct file *file);static int autofs4_dir_close(struct inode *inode, struct file *file);static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);static struct dentry *autofs4_root_lookup(struct inode *,struct dentry *, struct nameidata *);static int autofs4_dcache_readdir(struct file *, void *, filldir_t);struct file_operations autofs4_root_operations = {	.open		= dcache_dir_open,	.release	= dcache_dir_close,	.read		= generic_read_dir,	.readdir	= autofs4_root_readdir,	.ioctl		= autofs4_root_ioctl,};struct file_operations autofs4_dir_operations = {	.open		= autofs4_dir_open,	.release	= autofs4_dir_close,	.read		= generic_read_dir,	.readdir	= autofs4_dir_readdir,};struct inode_operations autofs4_root_inode_operations = {	.lookup		= autofs4_root_lookup,	.unlink		= autofs4_dir_unlink,	.symlink	= autofs4_dir_symlink,	.mkdir		= autofs4_dir_mkdir,	.rmdir		= autofs4_dir_rmdir,};struct inode_operations autofs4_dir_inode_operations = {	.lookup		= autofs4_dir_lookup,	.unlink		= autofs4_dir_unlink,	.symlink	= autofs4_dir_symlink,	.mkdir		= autofs4_dir_mkdir,	.rmdir		= autofs4_dir_rmdir,};static int autofs4_root_readdir(struct file *file, void *dirent,				filldir_t filldir){	struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);	int oz_mode = autofs4_oz_mode(sbi);	DPRINTK("called, filp->f_pos = %lld", file->f_pos);	/*	 * Don't set reghost flag if:	 * 1) f_pos is larger than zero -- we've already been here.	 * 2) we haven't even enabled reghosting in the 1st place.	 * 3) this is the daemon doing a readdir	 */	if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)		sbi->needs_reghost = 1;	DPRINTK("needs_reghost = %d", sbi->needs_reghost);	return autofs4_dcache_readdir(file, dirent, filldir);}/* Update usage from here to top of tree, so that scan of   top-level directories will give a useful result */static void autofs4_update_usage(struct dentry *dentry){	struct dentry *top = dentry->d_sb->s_root;	spin_lock(&dcache_lock);	for(; dentry != top; dentry = dentry->d_parent) {		struct autofs_info *ino = autofs4_dentry_ino(dentry);		if (ino) {			update_atime(dentry->d_inode);			ino->last_used = jiffies;		}	}	spin_unlock(&dcache_lock);}/* * From 2.4 kernel readdir.c */static int autofs4_dcache_readdir(struct file * filp, void * dirent, filldir_t filldir){	int i;	struct dentry *dentry = filp->f_dentry;	i = filp->f_pos;	switch (i) {		case 0:			if (filldir(dirent, ".", 1, i, dentry->d_inode->i_ino, DT_DIR) < 0)				break;			i++;			filp->f_pos++;			/* fallthrough */		case 1:			if (filldir(dirent, "..", 2, i, dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)				break;			i++;			filp->f_pos++;			/* fallthrough */		default: {			struct list_head *list;			int j = i-2;			spin_lock(&dcache_lock);			list = dentry->d_subdirs.next;			for (;;) {				if (list == &dentry->d_subdirs) {					spin_unlock(&dcache_lock);					return 0;				}				if (!j)					break;				j--;				list = list->next;			}			while(1) {				struct dentry *de = list_entry(list, struct dentry, d_child);				if (!d_unhashed(de) && de->d_inode) {					spin_unlock(&dcache_lock);					if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino, DT_UNKNOWN) < 0)						break;					spin_lock(&dcache_lock);				}				filp->f_pos++;				list = list->next;				if (list != &dentry->d_subdirs)					continue;				spin_unlock(&dcache_lock);				break;			}		}	}	return 0;}static int autofs4_dir_open(struct inode *inode, struct file *file){	struct dentry *dentry = file->f_dentry;	struct vfsmount *mnt = file->f_vfsmnt;	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);	int status;	DPRINTK("file=%p dentry=%p %.*s",		file, dentry, dentry->d_name.len, dentry->d_name.name);	if (autofs4_oz_mode(sbi))		goto out;	if (autofs4_ispending(dentry)) {		DPRINTK("dentry busy");		return -EBUSY;	}	if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {		struct nameidata nd;		int empty;		/* In case there are stale directory dentrys from a failed mount */		spin_lock(&dcache_lock);		empty = list_empty(&dentry->d_subdirs);		spin_unlock(&dcache_lock);		if (!empty)			d_invalidate(dentry);		nd.flags = LOOKUP_DIRECTORY;		status = (dentry->d_op->d_revalidate)(dentry, &nd);		if (!status)			return -ENOENT;	}	if (d_mountpoint(dentry)) {		struct file *fp = NULL;		struct vfsmount *fp_mnt = mntget(mnt);		struct dentry *fp_dentry = dget(dentry);		while (follow_down(&fp_mnt, &fp_dentry) && d_mountpoint(fp_dentry));		fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);		status = PTR_ERR(fp);		if (IS_ERR(fp)) {			file->private_data = NULL;			return status;		}		file->private_data = fp;	}out:	return 0;}static int autofs4_dir_close(struct inode *inode, struct file *file){	struct dentry *dentry = file->f_dentry;	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);	DPRINTK("file=%p dentry=%p %.*s",		file, dentry, dentry->d_name.len, dentry->d_name.name);	if (autofs4_oz_mode(sbi))		goto out;	if (autofs4_ispending(dentry)) {		DPRINTK("dentry busy");		return -EBUSY;	}	if (d_mountpoint(dentry)) {		struct file *fp = file->private_data;		if (!fp)			return -ENOENT;		filp_close(fp, current->files);		file->private_data = NULL;	}out:	return 0;}static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir){	struct dentry *dentry = file->f_dentry;	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);	int status;	DPRINTK("file=%p dentry=%p %.*s",		file, dentry, dentry->d_name.len, dentry->d_name.name);	if (autofs4_oz_mode(sbi))		goto out;	if (autofs4_ispending(dentry)) {		DPRINTK("dentry busy");		return -EBUSY;	}	if (d_mountpoint(dentry)) {		struct file *fp = file->private_data;		if (!fp)			return -ENOENT;		if (!fp->f_op || !fp->f_op->readdir)			goto out;		status = vfs_readdir(fp, filldir, dirent);		file->f_pos = fp->f_pos;		if (status)			autofs4_copy_atime(file, fp);		return status;	}out:	return autofs4_dcache_readdir(file, dirent, filldir);}static int try_to_fill_dentry(struct dentry *dentry, 			      struct super_block *sb,			      struct autofs_sb_info *sbi, int flags){	struct autofs_info *de_info = autofs4_dentry_ino(dentry);	int status = 0;	/* Block on any pending expiry here; invalidate the dentry           when expiration is done to trigger mount request with a new           dentry */	if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {		DPRINTK("waiting for expire %p name=%.*s",			 dentry, dentry->d_name.len, dentry->d_name.name);		status = autofs4_wait(sbi, dentry, NFY_NONE);				DPRINTK("expire done status=%d", status);				return 0;	}	DPRINTK("dentry=%p %.*s ino=%p",		 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);	/* Wait for a pending mount, triggering one if there isn't one already */	if (dentry->d_inode == NULL) {		DPRINTK("waiting for mount name=%.*s",			 dentry->d_name.len, dentry->d_name.name);		status = autofs4_wait(sbi, dentry, NFY_MOUNT);		 		DPRINTK("mount done status=%d", status);		if (status && dentry->d_inode)			return 0; /* Try to get the kernel to invalidate this dentry */				/* Turn this into a real negative dentry? */		if (status == -ENOENT) {			dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;			spin_lock(&dentry->d_lock);			dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;			spin_unlock(&dentry->d_lock);			return 1;		} else if (status) {			/* Return a negative dentry, but leave it "pending" */			return 1;		}	/* Trigger mount for path component or follow link */	} else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||			current->link_count) {		DPRINTK("waiting for mount name=%.*s",			dentry->d_name.len, dentry->d_name.name);		spin_lock(&dentry->d_lock);		dentry->d_flags |= DCACHE_AUTOFS_PENDING;		spin_unlock(&dentry->d_lock);		status = autofs4_wait(sbi, dentry, NFY_MOUNT);		DPRINTK("mount done status=%d", status);		if (status) {			spin_lock(&dentry->d_lock);			dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;			spin_unlock(&dentry->d_lock);			return 0;		}	}	/* We don't update the usages for the autofs daemon itself, this	   is necessary for recursive autofs mounts */	if (!autofs4_oz_mode(sbi))		autofs4_update_usage(dentry);	spin_lock(&dentry->d_lock);	dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;	spin_unlock(&dentry->d_lock);	return 1;}/* * Revalidate is called on every cache lookup.  Some of those * cache lookups may actually happen while the dentry is not * yet completely filled in, and revalidate has to delay such * lookups.. */static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd){	struct inode * dir = dentry->d_parent->d_inode;	struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);	int oz_mode = autofs4_oz_mode(sbi);	int flags = nd ? nd->flags : 0;	int status = 1;	/* Pending dentry */	if (autofs4_ispending(dentry)) {		if (!oz_mode)			status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);		return status;	}	/* Negative dentry.. invalidate if "old" */	if (dentry->d_inode == NULL)		return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);	/* Check for a non-mountpoint directory with no contents */	spin_lock(&dcache_lock);	if (S_ISDIR(dentry->d_inode->i_mode) &&	    !d_mountpoint(dentry) && 	    list_empty(&dentry->d_subdirs)) {		DPRINTK("dentry=%p %.*s, emptydir",			 dentry, dentry->d_name.len, dentry->d_name.name);		spin_unlock(&dcache_lock);		if (!oz_mode)			status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);		return status;	}	spin_unlock(&dcache_lock);	/* Update the usage list */	if (!oz_mode)		autofs4_update_usage(dentry);	return 1;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?