⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 namei.c

📁 ocfs1.2.7 源码
💻 C
📖 第 1 页 / 共 4 页
字号:
		goto bail;	/* check if the target already exists (in which case we need	 * to delete it */	status = ocfs2_find_files_on_disk(new_dentry->d_name.name,					  new_dentry->d_name.len,					  &newfe_blkno, new_dir, &new_de_bh,					  &new_de);	/* The only error we allow here is -ENOENT because the new	 * file not existing is perfectly valid. */	if ((status < 0) && (status != -ENOENT)) {		/* If we cannot find the file specified we should just */		/* return the error... */		mlog_errno(status);		goto bail;	}	if (!new_de && new_inode)		mlog(ML_ERROR, "inode %lu does not exist in it's parent "		     "directory!", new_inode->i_ino);	/* In case we need to overwrite an existing file, we blow it	 * away first */	if (new_de) {		/* VFS didn't think there existed an inode here, but		 * someone else in the cluster must have raced our		 * rename to create one. Today we error cleanly, in		 * the future we should consider calling iget to build		 * a new struct inode for this entry. */		if (!new_inode) {			status = -EACCES;			mlog(0, "We found an inode for name %.*s but VFS "			     "didn't give us one.\n", new_dentry->d_name.len,			     new_dentry->d_name.name);			goto bail;		}		if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) {			status = -EACCES;			mlog(0, "Inode blkno (%"MLFu64") and dir (%"MLFu64") "			     "disagree. ip_flags = %x\n",			     OCFS2_I(new_inode)->ip_blkno, newfe_blkno,			     OCFS2_I(new_inode)->ip_flags);			goto bail;		}		status = ocfs2_meta_lock(new_inode, handle, &newfe_bh, 1);		if (status < 0) {			if (status != -ENOENT)				mlog_errno(status);			goto bail;		}		if (S_ISDIR(new_inode->i_mode))			links_count = 0;		else			links_count = (unsigned int) (new_inode->i_nlink - 1);		status = ocfs2_request_unlink_vote(new_inode, new_dentry,						   links_count);		if (status < 0) {			mlog_errno(status);			goto bail;		}		newfe = (struct ocfs2_dinode *) newfe_bh->b_data;		mlog(0, "aha rename over existing... new_de=%p "		     "new_blkno=%"MLFu64" newfebh=%p bhblocknr=%llu\n",		     new_de, newfe_blkno, newfe_bh, newfe_bh ?		     (unsigned long long)newfe_bh->b_blocknr : 0ULL);		if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {			status = ocfs2_prepare_orphan_dir(osb, handle,							  new_inode,							  orphan_name,							  &orphan_entry_bh);			if (status < 0) {				mlog_errno(status);				goto bail;			}		}	} else {		BUG_ON(new_dentry->d_parent->d_inode != new_dir);		status = ocfs2_check_dir_for_entry(new_dir,						   new_dentry->d_name.name,						   new_dentry->d_name.len);		if (status)			goto bail;		status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh,						      new_dentry->d_name.name,						      new_dentry->d_name.len,						      &insert_entry_bh);		if (status < 0) {			mlog_errno(status);			goto bail;		}	}	handle = ocfs2_start_trans(osb, handle, OCFS2_RENAME_CREDITS);	if (IS_ERR(handle)) {		status = PTR_ERR(handle);		handle = NULL;		mlog_errno(status);		goto bail;	}	if (new_de) {		if (S_ISDIR(new_inode->i_mode)) {			if (!ocfs2_empty_dir(new_inode) ||			    new_inode->i_nlink != 2) {				status = -ENOTEMPTY;				goto bail;			}		}		status = ocfs2_journal_access(handle, new_inode, newfe_bh,					      OCFS2_JOURNAL_ACCESS_WRITE);		if (status < 0) {			mlog_errno(status);			goto bail;		}		if (S_ISDIR(new_inode->i_mode) ||		    (newfe->i_links_count == cpu_to_le16(1))){			status = ocfs2_orphan_add(osb, handle, new_inode,						  newfe, orphan_name,						  orphan_entry_bh);			if (status < 0) {				mlog_errno(status);				goto bail;			}		}		/* change the dirent to point to the correct inode */		status = ocfs2_journal_access(handle, new_dir, new_de_bh,					      OCFS2_JOURNAL_ACCESS_WRITE);		if (status < 0) {			mlog_errno(status);			goto bail;		}		new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno);		new_de->file_type = old_de->file_type;		new_dir->i_version++;		status = ocfs2_journal_dirty(handle, new_de_bh);		if (status < 0) {			mlog_errno(status);			goto bail;		}		if (S_ISDIR(new_inode->i_mode))			newfe->i_links_count = 0;		else			le16_add_cpu(&newfe->i_links_count, -1);		status = ocfs2_journal_dirty(handle, newfe_bh);		if (status < 0) {			mlog_errno(status);			goto bail;		}	} else {		/* if the name was not found in new_dir, add it now */		status = ocfs2_add_entry(handle, new_dentry, old_inode,					 OCFS2_I(old_inode)->ip_blkno,					 new_dir_bh, insert_entry_bh);	}	old_inode->i_ctime = CURRENT_TIME;	mark_inode_dirty(old_inode);	status = ocfs2_journal_access(handle, old_inode, old_inode_bh,				      OCFS2_JOURNAL_ACCESS_WRITE);	if (status >= 0) {		oldfe = (struct ocfs2_dinode *) old_inode_bh->b_data;		oldfe->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec);		oldfe->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec);		status = ocfs2_journal_dirty(handle, old_inode_bh);		if (status < 0)			mlog_errno(status);	} else		mlog_errno(status);	/* now that the name has been added to new_dir, remove the old name */	status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh);	if (status < 0) {		mlog_errno(status);		goto bail;	}	if (new_inode) {		new_inode->i_nlink--;		new_inode->i_ctime = CURRENT_TIME;	}	old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;	if (old_inode_de_bh) {		status = ocfs2_journal_access(handle, old_inode,					     old_inode_de_bh,					     OCFS2_JOURNAL_ACCESS_WRITE);		PARENT_INO(old_inode_de_bh->b_data) =			cpu_to_le64(OCFS2_I(new_dir)->ip_blkno);		status = ocfs2_journal_dirty(handle, old_inode_de_bh);		old_dir->i_nlink--;		if (new_inode) {			new_inode->i_nlink--;		} else {			new_dir->i_nlink++;			mark_inode_dirty(new_dir);		}	}	mark_inode_dirty(old_dir);	ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh);	if (new_inode) {		mark_inode_dirty(new_inode);		ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh);	}	if (old_dir != new_dir) {		/* Keep the same times on both directories.*/		new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime;		/*		 * This will also pick up the i_nlink change from the		 * block above.		 */		ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh);	}	if (old_dir_nlink != old_dir->i_nlink) {		if (!old_dir_bh) {			mlog(ML_ERROR, "need to change nlink for old dir "			     "%"MLFu64" from %d to %d but bh is NULL!\n",			     OCFS2_I(old_dir)->ip_blkno,			     (int)old_dir_nlink,			     old_dir->i_nlink);		} else {			struct ocfs2_dinode *fe;			status = ocfs2_journal_access(handle, old_dir,						      old_dir_bh,						      OCFS2_JOURNAL_ACCESS_WRITE);			fe = (struct ocfs2_dinode *) old_dir_bh->b_data;			fe->i_links_count = cpu_to_le16(old_dir->i_nlink);			status = ocfs2_journal_dirty(handle, old_dir_bh);		}	}	status = 0;bail:	if (rename_lock)		ocfs2_rename_unlock(osb);	if (handle)		ocfs2_commit_trans(handle);	if (new_inode)		sync_mapping_buffers(old_inode->i_mapping);	if (new_inode)		iput(new_inode);	if (newfe_bh)		brelse(newfe_bh);	if (old_inode_bh)		brelse(old_inode_bh);	if (old_dir_bh)		brelse(old_dir_bh);	if (new_dir_bh)		brelse(new_dir_bh);	if (new_de_bh)		brelse(new_de_bh);	if (old_de_bh)		brelse(old_de_bh);	if (old_inode_de_bh)		brelse(old_inode_de_bh);	if (orphan_entry_bh)		brelse(orphan_entry_bh);	if (insert_entry_bh)		brelse(insert_entry_bh);	mlog_exit(status);	return status;}/* * we expect i_size = strlen(symname). Copy symname into the file * data, including the null terminator. */static int ocfs2_create_symlink_data(struct ocfs2_super *osb,				     struct ocfs2_journal_handle *handle,				     struct inode *inode,				     const char *symname){	struct buffer_head **bhs = NULL;	const char *c;	struct super_block *sb = osb->sb;	u64 p_blkno;	int p_blocks;	int virtual, blocks, status, i, bytes_left;	bytes_left = i_size_read(inode) + 1;	/* we can't trust i_blocks because we're actually going to	 * write i_size + 1 bytes. */	blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;	mlog_entry("i_blocks = %lu, i_size = %llu, blocks = %d\n",		   (unsigned long)inode->i_blocks, i_size_read(inode), blocks);	/* Sanity check -- make sure we're going to fit. */	if (bytes_left >	    ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {		status = -EIO;		mlog_errno(status);		goto bail;	}	bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);	if (!bhs) {		status = -ENOMEM;		mlog_errno(status);		goto bail;	}	status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,					     &p_blocks);	if (status < 0) {		mlog_errno(status);		goto bail;	}	/* links can never be larger than one cluster so we know this	 * is all going to be contiguous, but do a sanity check	 * anyway. */	if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {		status = -EIO;		mlog_errno(status);		goto bail;	}	virtual = 0;	while(bytes_left > 0) {		c = &symname[virtual * sb->s_blocksize];		bhs[virtual] = sb_getblk(sb, p_blkno);		if (!bhs[virtual]) {			status = -ENOMEM;			mlog_errno(status);			goto bail;		}		ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);		status = ocfs2_journal_access(handle, inode, bhs[virtual],					      OCFS2_JOURNAL_ACCESS_CREATE);		if (status < 0) {			mlog_errno(status);			goto bail;		}		memset(bhs[virtual]->b_data, 0, sb->s_blocksize);		memcpy(bhs[virtual]->b_data, c,		       (bytes_left > sb->s_blocksize) ? sb->s_blocksize :		       bytes_left);		status = ocfs2_journal_dirty(handle, bhs[virtual]);		if (status < 0) {			mlog_errno(status);			goto bail;		}		virtual++;		p_blkno++;		bytes_left -= sb->s_blocksize;	}	status = 0;bail:	if (bhs) {		for(i = 0; i < blocks; i++)			if (bhs[i])				brelse(bhs[i]);		kfree(bhs);	}	mlog_exit(status);	return status;}static int ocfs2_symlink(struct inode *dir,			 struct dentry *dentry,			 const char *symname){	int status, l, credits;	u64 newsize;	struct ocfs2_super *osb = NULL;	struct inode *inode = NULL;	struct super_block *sb;	struct buffer_head *new_fe_bh = NULL;	struct buffer_head *de_bh = NULL;	struct buffer_head *parent_fe_bh = NULL;	struct ocfs2_dinode *fe = NULL;	struct ocfs2_dinode *dirfe;	struct ocfs2_journal_handle *handle = NULL;	struct ocfs2_alloc_context *inode_ac = NULL;	struct ocfs2_alloc_context *data_ac = NULL;	mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,		   dentry, symname, dentry->d_name.len, dentry->d_name.name);	sb = dir->i_sb;	osb = OCFS2_SB(sb);	l = strlen(symname) + 1;	credits = ocfs2_calc_symlink_credits(sb);	handle = ocfs2_alloc_handle(osb);	if (handle == NULL) {		status = -ENOMEM;		mlog_errno(status);		goto bail;	}	/* lock the parent directory */	status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);	if (status < 0) {		if (status != -ENOENT)			mlog_errno(status);		goto bail;	}	dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;	if (!dirfe->i_links_count) {		/* can't make a file in a deleted directory. */		status = -ENOENT;		goto bail;	}	status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,					   dentry->d_name.len);	if (status)		goto bail;	status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,					      dentry->d_name.name,					      dentry->d_name.len, &de_bh);	if (status < 0) {		mlog_errno(status);		goto bail;	}	status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);	if (status < 0) {		if (status != -ENOSPC)			mlog_errno(status);		goto bail;	}	/* don't reserve bitmap space for fast symlinks. */	if (l > ocfs2_fast_symlink_chars(sb)) {		status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);		if (status < 0) {			if (status != -ENOSPC)				mlog_errno(status);			goto bail;		}	}	handle = ocfs2_start_trans(osb, handle, credits);	if (IS_ERR(handle)) {		status = PTR_ERR(handle);		handle = NULL;		mlog_errno(status);		goto bail;	}	status = ocfs2_mknod_locked(osb, dir, dentry,				    S_IFLNK | S_IRWXUGO, 0,				    &new_fe_bh, parent_fe_bh, handle,				    &inode, inode_ac);	if (status < 0) {		mlog_errno(status);		goto bail;	}	fe = (struct ocfs2_dinode *) new_fe_bh->b_data;	inode->i_rdev = 0;	newsize = l - 1;	if (l > ocfs2_fast_symlink_chars(sb)) {		inode->i_op = &ocfs2_symlink_inode_operations;		status = ocfs2_extend_allocation(osb, inode, 1, new_fe_bh,						 handle, data_ac, NULL, NULL);		if (status < 0) {			if (status != -ENOSPC && status != -EINTR) {				mlog(ML_ERROR, "Failed to extend file to "					       "%"MLFu64"\n",				     newsize);				mlog_errno(status);				status = -ENOSPC;			}			goto bail;		}		i_size_write(inode, newsize);		inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);	} else {		inode->i_op = &ocfs2_fast_symlink_inode_operations;		memcpy((char *) fe->id2.i_symlink, symname, l);		i_size_write(inode, newsize);		inode->i_blocks = 0;	}	status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);	if (status < 0) {		mlog_errno(status);		goto bail;	}	if (!ocfs2_inode_is_fast_symlink(inode)) {		status = ocfs2_create_symlink_data(osb, handle, inode,						   symname);		if (status < 0) {			mlog_errno(status);			goto bail;		}	}	status = ocfs2_add_entry(handle, dentry, inode,				 le64_to_cpu(fe->i_blkno), parent_fe_bh,				 de_bh);	if (status < 0) {		mlog_errno(status);		goto bail;	}	insert_inode_hash(inode);	dentry->d_op = &ocfs2_dentry_ops;	d_instantiate(dentry, inode);bail:	if (handle)		ocfs2_commit_trans(handle);	if (new_fe_bh)		brelse(new_fe_bh);	if (parent_fe_bh)		brelse(parent_fe_bh);	if (de_bh)		brelse(de_bh);	if (inode_ac)		ocfs2_free_alloc_context(inode_ac);	if (data_ac)		ocfs2_free_alloc_context(data_ac);	if ((status < 0) && inode)		iput(inode);	mlog_exit(status);	return status;}int ocfs2_check_dir_entry(struct inode * dir,			  struct ocfs2_dir_entry * de,			  struct buffer_head * bh,			  unsigned long offset){	const char *error_msg = NULL;

⌨️ 快捷键说明

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