📄 namei.c
字号:
BUG(); } ocfs2_inode_set_new(osb, inode); if (!ocfs2_mount_local(osb)) { status = ocfs2_create_new_inode_locks(inode); if (status < 0) mlog_errno(status); } status = 0; /* error in ocfs2_create_new_inode_locks is not * critical */ *ret_inode = inode;leave: if (status < 0) { if (*new_fe_bh) { brelse(*new_fe_bh); *new_fe_bh = NULL; } if (inode) iput(inode); } mlog_exit(status); return status;}static int ocfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode){ int ret; mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode, dentry->d_name.len, dentry->d_name.name); ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0); mlog_exit(ret); return ret;}static int ocfs2_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd){ int ret; mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode, dentry->d_name.len, dentry->d_name.name); ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0); mlog_exit(ret); return ret;}static int ocfs2_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry){ struct ocfs2_journal_handle *handle = NULL; struct inode *inode = old_dentry->d_inode; int err; struct buffer_head *fe_bh = NULL; struct buffer_head *parent_fe_bh = NULL; struct buffer_head *de_bh = NULL; struct ocfs2_dinode *fe = NULL; struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino, old_dentry->d_name.len, old_dentry->d_name.name, dentry->d_name.len, dentry->d_name.name); if (S_ISDIR(inode->i_mode)) { err = -EPERM; goto bail; } if (inode->i_nlink >= OCFS2_LINK_MAX) { err = -EMLINK; goto bail; } handle = ocfs2_alloc_handle(osb); if (handle == NULL) { err = -ENOMEM; goto bail; } err = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1); if (err < 0) { if (err != -ENOENT) mlog_errno(err); goto bail; } err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, dentry->d_name.len); if (err) goto bail; err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, dentry->d_name.name, dentry->d_name.len, &de_bh); if (err < 0) { mlog_errno(err); goto bail; } err = ocfs2_meta_lock(inode, handle, &fe_bh, 1); if (err < 0) { if (err != -ENOENT) mlog_errno(err); goto bail; } fe = (struct ocfs2_dinode *) fe_bh->b_data; if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) { err = -EMLINK; goto bail; } handle = ocfs2_start_trans(osb, handle, OCFS2_LINK_CREDITS); if (IS_ERR(handle)) { err = PTR_ERR(handle); handle = NULL; mlog_errno(err); goto bail; } err = ocfs2_journal_access(handle, inode, fe_bh, OCFS2_JOURNAL_ACCESS_WRITE); if (err < 0) { mlog_errno(err); goto bail; } inode->i_nlink++; inode->i_ctime = CURRENT_TIME; fe->i_links_count = cpu_to_le16(inode->i_nlink); fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); err = ocfs2_journal_dirty(handle, fe_bh); if (err < 0) { le16_add_cpu(&fe->i_links_count, -1); inode->i_nlink--; mlog_errno(err); goto bail; } err = ocfs2_add_entry(handle, dentry, inode, OCFS2_I(inode)->ip_blkno, parent_fe_bh, de_bh); if (err) { le16_add_cpu(&fe->i_links_count, -1); inode->i_nlink--; mlog_errno(err); goto bail; } atomic_inc(&inode->i_count); dentry->d_op = &ocfs2_dentry_ops; d_instantiate(dentry, inode);bail: if (handle) ocfs2_commit_trans(handle); 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;}static int ocfs2_unlink(struct inode *dir, struct dentry *dentry){ int status; unsigned int saved_nlink = 0; struct inode *inode = dentry->d_inode; 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; struct ocfs2_journal_handle *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 = %"MLFu64"\n", OCFS2_I(inode)->ip_blkno); if (inode == osb->root_inode) { mlog(0, "Cannot delete the root directory\n"); status = -EPERM; goto leave; } handle = ocfs2_alloc_handle(osb); if (handle == NULL) { status = -ENOMEM; mlog_errno(status); goto leave; } status = ocfs2_meta_lock(dir, handle, &parent_node_bh, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto leave; } 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 (%"MLFu64") != dirent blkno (%"MLFu64") " "ip_flags = %x\n", OCFS2_I(inode)->ip_blkno, blkno, OCFS2_I(inode)->ip_flags); goto leave; } status = ocfs2_meta_lock(inode, handle, &fe_bh, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto leave; } 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; } } /* There are still a few steps left until we can consider the * unlink to have succeeded. Save off nlink here before * modification so we can set it back in case we hit an issue * before commit. */ saved_nlink = inode->i_nlink; if (S_ISDIR(inode->i_mode)) inode->i_nlink = 0; else inode->i_nlink--; status = ocfs2_request_unlink_vote(inode, dentry, (unsigned int) inode->i_nlink); if (status < 0) { /* This vote should succeed under all normal * circumstances. */ mlog_errno(status); goto leave; } if (!inode->i_nlink) { status = ocfs2_prepare_orphan_dir(osb, handle, inode, orphan_name, &orphan_entry_bh); if (status < 0) { mlog_errno(status); goto leave; } } handle = ocfs2_start_trans(osb, handle, 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->i_nlink) { status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name, orphan_entry_bh); 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; } /* We can set nlink on the dinode now. clear the saved version * so that it doesn't get set later. */ fe->i_links_count = cpu_to_le16(inode->i_nlink); saved_nlink = 0; 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)) dir->i_nlink--; status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh); if (status < 0) { mlog_errno(status); if (S_ISDIR(inode->i_mode)) dir->i_nlink++; }leave: if (status < 0 && saved_nlink) inode->i_nlink = saved_nlink; if (handle) ocfs2_commit_trans(handle); 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 ocfs2_journal_handle *handle, 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 = %"MLFu64", inode2 = %"MLFu64")\n", oi1->ip_blkno, oi2->ip_blkno); BUG_ON(!handle); 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, handle, bh2, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto bail; } } /* lock id1 */ status = ocfs2_meta_lock(inode1, handle, bh1, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto bail; }bail: mlog_exit(status); return status;}#define PARENT_INO(buffer) \ ((struct ocfs2_dir_entry *) \ ((char *)buffer + \ le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inodestatic 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; struct inode *old_inode = old_dentry->d_inode; struct inode *new_inode = new_dentry->d_inode; struct ocfs2_dinode *oldfe = 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; struct ocfs2_journal_handle *handle = NULL; struct buffer_head *old_dir_bh = NULL; struct buffer_head *new_dir_bh = NULL; struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry // and new_dentry 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; unsigned int links_count; /* 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 heirarchy 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; } handle = ocfs2_alloc_handle(osb); if (handle == NULL) { status = -ENOMEM; mlog_errno(status); goto bail; } /* if old and new are the same, this'll just do one lock. */ status = ocfs2_double_lock(osb, handle, &old_dir_bh, old_dir, &new_dir_bh, new_dir); if (status < 0) { mlog_errno(status); goto bail; } /* 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; } } status = ocfs2_meta_lock(old_inode, handle, &old_inode_bh, 1); if (status < 0) { if (status != -ENOENT) mlog_errno(status); goto bail; } status = ocfs2_request_rename_vote(old_inode, old_dentry); if (status < 0) { mlog_errno(status); goto bail; } if (S_ISDIR(old_inode->i_mode)) { status = -EIO; old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0); if (!old_inode_de_bh) goto bail; status = -EIO; if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) != OCFS2_I(old_dir)->ip_blkno) goto bail; status = -EMLINK; if (!new_inode && new_dir!=old_dir && new_dir->i_nlink >= OCFS2_LINK_MAX) goto bail; } status = -ENOENT; old_de_bh = ocfs2_find_entry(old_dentry->d_name.name, old_dentry->d_name.len, old_dir, &old_de); if (!old_de_bh) 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 (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -