📄 namei.c
字号:
out_commit: ocfs2_commit_trans(osb, handle);out_unlock_inode: ocfs2_meta_unlock(inode, 1);out: ocfs2_meta_unlock(dir, 1); if (de_bh) brelse(de_bh); if (fe_bh) brelse(fe_bh); if (parent_fe_bh) brelse(parent_fe_bh); mlog_exit(err); return err;}/* * Takes and drops an exclusive lock on the given dentry. This will * force other nodes to drop it. */static int ocfs2_remote_dentry_delete(struct dentry *dentry){ int ret; ret = ocfs2_dentry_lock(dentry, 1); if (ret) mlog_errno(ret); else ocfs2_dentry_unlock(dentry, 1); return ret;}static inline int inode_is_unlinkable(struct inode *inode){ if (S_ISDIR(inode->i_mode)) { if (inode->i_nlink == 2) return 1; return 0; } if (inode->i_nlink == 1) return 1; return 0;}static int ocfs2_unlink(struct inode *dir, struct dentry *dentry){ int status; int child_locked = 0; struct inode *inode = dentry->d_inode; struct inode *orphan_dir = NULL; struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); u64 blkno; struct ocfs2_dinode *fe = NULL; struct buffer_head *fe_bh = NULL; struct buffer_head *parent_node_bh = NULL; handle_t *handle = NULL; struct ocfs2_dir_entry *dirent = NULL; struct buffer_head *dirent_bh = NULL; char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; struct buffer_head *orphan_entry_bh = NULL; mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry, dentry->d_name.len, dentry->d_name.name); BUG_ON(dentry->d_parent->d_inode != dir); mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno); if (inode == osb->root_inode) { mlog(0, "Cannot delete the root directory\n"); return -EPERM; } status = ocfs2_meta_lock(dir, &parent_node_bh, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); return status; } status = ocfs2_find_files_on_disk(dentry->d_name.name, dentry->d_name.len, &blkno, dir, &dirent_bh, &dirent); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto leave; } if (OCFS2_I(inode)->ip_blkno != blkno) { status = -ENOENT; mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n", (unsigned long long)OCFS2_I(inode)->ip_blkno, (unsigned long long)blkno, OCFS2_I(inode)->ip_flags); goto leave; } status = ocfs2_meta_lock(inode, &fe_bh, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto leave; } child_locked = 1; if (S_ISDIR(inode->i_mode)) { if (!ocfs2_empty_dir(inode)) { status = -ENOTEMPTY; goto leave; } else if (inode->i_nlink != 2) { status = -ENOTEMPTY; goto leave; } } status = ocfs2_remote_dentry_delete(dentry); if (status < 0) { /* This vote should succeed under all normal * circumstances. */ mlog_errno(status); goto leave; } if (inode_is_unlinkable(inode)) { status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode, orphan_name, &orphan_entry_bh); if (status < 0) { mlog_errno(status); goto leave; } } handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS); if (IS_ERR(handle)) { status = PTR_ERR(handle); handle = NULL; mlog_errno(status); goto leave; } status = ocfs2_journal_access(handle, inode, fe_bh, OCFS2_JOURNAL_ACCESS_WRITE); if (status < 0) { mlog_errno(status); goto leave; } fe = (struct ocfs2_dinode *) fe_bh->b_data; if (inode_is_unlinkable(inode)) { status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name, orphan_entry_bh, orphan_dir); if (status < 0) { mlog_errno(status); goto leave; } } /* delete the name from the parent dir */ status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh); if (status < 0) { mlog_errno(status); goto leave; } if (S_ISDIR(inode->i_mode)) drop_nlink(inode); drop_nlink(inode); fe->i_links_count = cpu_to_le16(inode->i_nlink); status = ocfs2_journal_dirty(handle, fe_bh); if (status < 0) { mlog_errno(status); goto leave; } dir->i_ctime = dir->i_mtime = CURRENT_TIME; if (S_ISDIR(inode->i_mode)) drop_nlink(dir); status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh); if (status < 0) { mlog_errno(status); if (S_ISDIR(inode->i_mode)) inc_nlink(dir); }leave: if (handle) ocfs2_commit_trans(osb, handle); if (child_locked) ocfs2_meta_unlock(inode, 1); ocfs2_meta_unlock(dir, 1); if (orphan_dir) { /* This was locked for us in ocfs2_prepare_orphan_dir() */ ocfs2_meta_unlock(orphan_dir, 1); mutex_unlock(&orphan_dir->i_mutex); iput(orphan_dir); } if (fe_bh) brelse(fe_bh); if (dirent_bh) brelse(dirent_bh); if (parent_node_bh) brelse(parent_node_bh); if (orphan_entry_bh) brelse(orphan_entry_bh); mlog_exit(status); return status;}/* * The only place this should be used is rename! * if they have the same id, then the 1st one is the only one locked. */static int ocfs2_double_lock(struct ocfs2_super *osb, struct buffer_head **bh1, struct inode *inode1, struct buffer_head **bh2, struct inode *inode2){ int status; struct ocfs2_inode_info *oi1 = OCFS2_I(inode1); struct ocfs2_inode_info *oi2 = OCFS2_I(inode2); struct buffer_head **tmpbh; struct inode *tmpinode; mlog_entry("(inode1 = %llu, inode2 = %llu)\n", (unsigned long long)oi1->ip_blkno, (unsigned long long)oi2->ip_blkno); if (*bh1) *bh1 = NULL; if (*bh2) *bh2 = NULL; /* we always want to lock the one with the lower lockid first. */ if (oi1->ip_blkno != oi2->ip_blkno) { if (oi1->ip_blkno < oi2->ip_blkno) { /* switch id1 and id2 around */ mlog(0, "switching them around...\n"); tmpbh = bh2; bh2 = bh1; bh1 = tmpbh; tmpinode = inode2; inode2 = inode1; inode1 = tmpinode; } /* lock id2 */ status = ocfs2_meta_lock(inode2, bh2, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto bail; } } /* lock id1 */ status = ocfs2_meta_lock(inode1, bh1, 1); if (status < 0) { /* * An error return must mean that no cluster locks * were held on function exit. */ if (oi1->ip_blkno != oi2->ip_blkno) ocfs2_meta_unlock(inode2, 1); if (status != -ENOENT) mlog_errno(status); }bail: mlog_exit(status); return status;}static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2){ ocfs2_meta_unlock(inode1, 1); if (inode1 != inode2) ocfs2_meta_unlock(inode2, 1);}static int ocfs2_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry){ int status = 0, rename_lock = 0, parents_locked = 0; int old_child_locked = 0, new_child_locked = 0; struct inode *old_inode = old_dentry->d_inode; struct inode *new_inode = new_dentry->d_inode; struct inode *orphan_dir = NULL; struct ocfs2_dinode *newfe = NULL; char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; struct buffer_head *orphan_entry_bh = NULL; struct buffer_head *newfe_bh = NULL; struct buffer_head *old_inode_bh = NULL; struct buffer_head *insert_entry_bh = NULL; struct ocfs2_super *osb = NULL; u64 newfe_blkno, old_de_ino; handle_t *handle = NULL; struct buffer_head *old_dir_bh = NULL; 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_dentry != new_dentry) { 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 vote thread on other nodes won't have * to concurrently downconvert the inode and the dentry locks. */ status = ocfs2_meta_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_meta_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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -