📄 namei.c
字号:
struct buffer_head *new_dir_bh = NULL; struct ocfs2_dir_entry *old_inode_dot_dot_de = NULL, *old_de = NULL, *new_de = NULL; struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir, // this is the 1st dirent bh nlink_t old_dir_nlink = old_dir->i_nlink; struct ocfs2_dinode *old_di; /* At some point it might be nice to break this function up a * bit. */ mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n", old_dir, old_dentry, new_dir, new_dentry, old_dentry->d_name.len, old_dentry->d_name.name, new_dentry->d_name.len, new_dentry->d_name.name); osb = OCFS2_SB(old_dir->i_sb); if (new_inode) { if (!igrab(new_inode)) BUG(); } /* Assume a directory hierarchy thusly: * a/b/c * a/d * a,b,c, and d are all directories. * * from cwd of 'a' on both nodes: * node1: mv b/c d * node2: mv d b/c * * And that's why, just like the VFS, we need a file system * rename lock. */ if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) { status = ocfs2_rename_lock(osb); if (status < 0) { mlog_errno(status); goto bail; } rename_lock = 1; } /* if old and new are the same, this'll just do one lock. */ status = ocfs2_double_lock(osb, &old_dir_bh, old_dir, &new_dir_bh, new_dir); if (status < 0) { mlog_errno(status); goto bail; } parents_locked = 1; /* make sure both dirs have bhs * get an extra ref on old_dir_bh if old==new */ if (!new_dir_bh) { if (old_dir_bh) { new_dir_bh = old_dir_bh; get_bh(new_dir_bh); } else { mlog(ML_ERROR, "no old_dir_bh!\n"); status = -EIO; goto bail; } } /* * Aside from allowing a meta data update, the locking here * also ensures that the downconvert thread on other nodes * won't have to concurrently downconvert the inode and the * dentry locks. */ status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto bail; } old_child_locked = 1; status = ocfs2_remote_dentry_delete(old_dentry); if (status < 0) { mlog_errno(status); goto bail; } if (S_ISDIR(old_inode->i_mode)) { u64 old_inode_parent; status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent, old_inode, &old_inode_de_bh, &old_inode_dot_dot_de); if (status) { status = -EIO; goto bail; } if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) { status = -EIO; goto bail; } if (!new_inode && new_dir != old_dir && new_dir->i_nlink >= OCFS2_LINK_MAX) { status = -EMLINK; goto bail; } } status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name, old_dentry->d_name.len, &old_de_ino); if (status) { status = -ENOENT; goto bail; } /* * Check for inode number is _not_ due to possible IO errors. * We might rmdir the source, keep it as pwd of some process * and merrily kill the link to whatever was created under the * same name. Goodbye sticky bit ;-< */ if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) { status = -ENOENT; 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) { /* * Target was unlinked by another node while we were * waiting to get to ocfs2_rename(). There isn't * anything we can do here to help the situation, so * bubble up the appropriate error. */ status = -ENOENT; goto bail; } /* 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 %llu and dir %llu disagree. flags = %x\n", (unsigned long long)OCFS2_I(new_inode)->ip_blkno, (unsigned long long)newfe_blkno, OCFS2_I(new_inode)->ip_flags); goto bail; } status = ocfs2_inode_lock(new_inode, &newfe_bh, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto bail; } new_child_locked = 1; status = ocfs2_remote_dentry_delete(new_dentry); 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=%llu " "newfebh=%p bhblocknr=%llu\n", new_de, (unsigned long long)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, &orphan_dir, 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, 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, orphan_dir); if (status < 0) { mlog_errno(status); goto bail; } } /* change the dirent to point to the correct inode */ status = ocfs2_update_entry(new_dir, handle, new_de_bh, new_de, old_inode); if (status < 0) { mlog_errno(status); goto bail; } new_dir->i_version++; 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) { old_di = (struct ocfs2_dinode *) old_inode_bh->b_data; old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec); old_di->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. * * We don't keep any directory entry context around until now * because the insert might have changed the type of directory * we're dealing with. */ old_de_bh = ocfs2_find_entry(old_dentry->d_name.name, old_dentry->d_name.len, old_dir, &old_de); if (!old_de_bh) { status = -EIO; goto bail; } 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_update_entry(old_inode, handle, old_inode_de_bh, old_inode_dot_dot_de, new_dir); old_dir->i_nlink--; if (new_inode) { new_inode->i_nlink--; } else { inc_nlink(new_dir); 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 " "%llu from %d to %d but bh is NULL!\n", (unsigned long long)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); } } ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir); status = 0;bail: if (rename_lock) ocfs2_rename_unlock(osb); if (handle) ocfs2_commit_trans(osb, handle); if (parents_locked) ocfs2_double_unlock(old_dir, new_dir); if (old_child_locked) ocfs2_inode_unlock(old_inode, 1); if (new_child_locked) ocfs2_inode_unlock(new_inode, 1); if (orphan_dir) { /* This was locked for us in ocfs2_prepare_orphan_dir() */ ocfs2_inode_unlock(orphan_dir, 1); mutex_unlock(&orphan_dir->i_mutex); iput(orphan_dir); } 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, handle_t *handle, struct inode *inode, const char *symname){ struct buffer_head **bhs = NULL; const char *c; struct super_block *sb = osb->sb; u64 p_blkno, 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 = %llu, i_size = %llu, blocks = %d\n", (unsigned long 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, &p_blkno, &p_blocks, NULL); 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -