Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * namei.c |
| 5 | * |
| 6 | * Create and rename file, directory, symlinks |
| 7 | * |
| 8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. |
| 9 | * |
| 10 | * Portions of this code from linux/fs/ext3/dir.c |
| 11 | * |
| 12 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 13 | * Remy Card (card@masi.ibp.fr) |
| 14 | * Laboratoire MASI - Institut Blaise pascal |
| 15 | * Universite Pierre et Marie Curie (Paris VI) |
| 16 | * |
| 17 | * from |
| 18 | * |
| 19 | * linux/fs/minix/dir.c |
| 20 | * |
| 21 | * Copyright (C) 1991, 1992 Linux Torvalds |
| 22 | * |
| 23 | * This program is free software; you can redistribute it and/or |
| 24 | * modify it under the terms of the GNU General Public |
| 25 | * License as published by the Free Software Foundation; either |
| 26 | * version 2 of the License, or (at your option) any later version. |
| 27 | * |
| 28 | * This program is distributed in the hope that it will be useful, |
| 29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 31 | * General Public License for more details. |
| 32 | * |
| 33 | * You should have received a copy of the GNU General Public |
| 34 | * License along with this program; if not, write to the |
| 35 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 36 | * Boston, MA 021110-1307, USA. |
| 37 | */ |
| 38 | |
| 39 | #include <linux/fs.h> |
| 40 | #include <linux/types.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/highmem.h> |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 43 | #include <linux/quotaops.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 44 | |
| 45 | #define MLOG_MASK_PREFIX ML_NAMEI |
| 46 | #include <cluster/masklog.h> |
| 47 | |
| 48 | #include "ocfs2.h" |
| 49 | |
| 50 | #include "alloc.h" |
| 51 | #include "dcache.h" |
| 52 | #include "dir.h" |
| 53 | #include "dlmglue.h" |
| 54 | #include "extent_map.h" |
| 55 | #include "file.h" |
| 56 | #include "inode.h" |
| 57 | #include "journal.h" |
| 58 | #include "namei.h" |
| 59 | #include "suballoc.h" |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 60 | #include "super.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 61 | #include "symlink.h" |
| 62 | #include "sysfile.h" |
| 63 | #include "uptodate.h" |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 64 | #include "xattr.h" |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 65 | #include "acl.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 66 | |
| 67 | #include "buffer_head_io.h" |
| 68 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 69 | static int ocfs2_mknod_locked(struct ocfs2_super *osb, |
| 70 | struct inode *dir, |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 71 | struct inode *inode, |
| 72 | struct dentry *dentry, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 73 | dev_t dev, |
| 74 | struct buffer_head **new_fe_bh, |
| 75 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 76 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 77 | struct ocfs2_alloc_context *inode_ac); |
| 78 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 79 | static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 80 | struct inode **ret_orphan_dir, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 81 | struct inode *inode, |
| 82 | char *name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 83 | struct ocfs2_dir_lookup_result *lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 84 | |
| 85 | static int ocfs2_orphan_add(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 86 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 87 | struct inode *inode, |
| 88 | struct ocfs2_dinode *fe, |
| 89 | char *name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 90 | struct ocfs2_dir_lookup_result *lookup, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 91 | struct inode *orphan_dir_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 92 | |
| 93 | static int ocfs2_create_symlink_data(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 94 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 95 | struct inode *inode, |
| 96 | const char *symname); |
| 97 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 98 | /* An orphan dir name is an 8 byte value, printed as a hex string */ |
| 99 | #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64))) |
| 100 | |
| 101 | static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry, |
| 102 | struct nameidata *nd) |
| 103 | { |
| 104 | int status; |
| 105 | u64 blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 106 | struct inode *inode = NULL; |
| 107 | struct dentry *ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 108 | struct ocfs2_inode_info *oi; |
| 109 | |
| 110 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry, |
| 111 | dentry->d_name.len, dentry->d_name.name); |
| 112 | |
| 113 | if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) { |
| 114 | ret = ERR_PTR(-ENAMETOOLONG); |
| 115 | goto bail; |
| 116 | } |
| 117 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 118 | mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len, |
| 119 | dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 120 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 121 | status = ocfs2_inode_lock(dir, NULL, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 122 | if (status < 0) { |
| 123 | if (status != -ENOENT) |
| 124 | mlog_errno(status); |
| 125 | ret = ERR_PTR(status); |
| 126 | goto bail; |
| 127 | } |
| 128 | |
Mark Fasheh | be94d11 | 2007-09-11 15:22:06 -0700 | [diff] [blame] | 129 | status = ocfs2_lookup_ino_from_name(dir, dentry->d_name.name, |
| 130 | dentry->d_name.len, &blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 131 | if (status < 0) |
| 132 | goto bail_add; |
| 133 | |
Jan Kara | 5fa0613 | 2008-01-11 00:11:45 +0100 | [diff] [blame] | 134 | inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 135 | if (IS_ERR(inode)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 136 | ret = ERR_PTR(-EACCES); |
| 137 | goto bail_unlock; |
| 138 | } |
| 139 | |
| 140 | oi = OCFS2_I(inode); |
| 141 | /* Clear any orphaned state... If we were able to look up the |
| 142 | * inode from a directory, it certainly can't be orphaned. We |
| 143 | * might have the bad state from a node which intended to |
| 144 | * orphan this inode but crashed before it could commit the |
| 145 | * unlink. */ |
| 146 | spin_lock(&oi->ip_lock); |
| 147 | oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 148 | spin_unlock(&oi->ip_lock); |
| 149 | |
| 150 | bail_add: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 151 | dentry->d_op = &ocfs2_dentry_ops; |
| 152 | ret = d_splice_alias(inode, dentry); |
| 153 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 154 | if (inode) { |
| 155 | /* |
| 156 | * If d_splice_alias() finds a DCACHE_DISCONNECTED |
| 157 | * dentry, it will d_move() it on top of ourse. The |
| 158 | * return value will indicate this however, so in |
| 159 | * those cases, we switch them around for the locking |
| 160 | * code. |
| 161 | * |
| 162 | * NOTE: This dentry already has ->d_op set from |
| 163 | * ocfs2_get_parent() and ocfs2_get_dentry() |
| 164 | */ |
| 165 | if (ret) |
| 166 | dentry = ret; |
| 167 | |
| 168 | status = ocfs2_dentry_attach_lock(dentry, inode, |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 169 | OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 170 | if (status) { |
| 171 | mlog_errno(status); |
| 172 | ret = ERR_PTR(status); |
| 173 | goto bail_unlock; |
| 174 | } |
| 175 | } |
| 176 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 177 | bail_unlock: |
| 178 | /* Don't drop the cluster lock until *after* the d_add -- |
| 179 | * unlink on another node will message us to remove that |
| 180 | * dentry under this lock so otherwise we can race this with |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 181 | * the downconvert thread and have a stale dentry. */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 182 | ocfs2_inode_unlock(dir, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 183 | |
| 184 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 185 | |
| 186 | mlog_exit_ptr(ret); |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 191 | static struct inode *ocfs2_get_init_inode(struct inode *dir, int mode) |
| 192 | { |
| 193 | struct inode *inode; |
| 194 | |
| 195 | inode = new_inode(dir->i_sb); |
| 196 | if (!inode) { |
| 197 | mlog(ML_ERROR, "new_inode failed!\n"); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | /* populate as many fields early on as possible - many of |
| 202 | * these are used by the support functions here and in |
| 203 | * callers. */ |
| 204 | if (S_ISDIR(mode)) |
| 205 | inode->i_nlink = 2; |
| 206 | else |
| 207 | inode->i_nlink = 1; |
| 208 | inode->i_uid = current_fsuid(); |
| 209 | if (dir->i_mode & S_ISGID) { |
| 210 | inode->i_gid = dir->i_gid; |
| 211 | if (S_ISDIR(mode)) |
| 212 | mode |= S_ISGID; |
| 213 | } else |
| 214 | inode->i_gid = current_fsgid(); |
| 215 | inode->i_mode = mode; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 216 | vfs_dq_init(inode); |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 217 | return inode; |
| 218 | } |
| 219 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 220 | static int ocfs2_mknod(struct inode *dir, |
| 221 | struct dentry *dentry, |
| 222 | int mode, |
| 223 | dev_t dev) |
| 224 | { |
| 225 | int status = 0; |
| 226 | struct buffer_head *parent_fe_bh = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 227 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 228 | struct ocfs2_super *osb; |
| 229 | struct ocfs2_dinode *dirfe; |
| 230 | struct buffer_head *new_fe_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 231 | struct inode *inode = NULL; |
| 232 | struct ocfs2_alloc_context *inode_ac = NULL; |
| 233 | struct ocfs2_alloc_context *data_ac = NULL; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 234 | struct ocfs2_alloc_context *meta_ac = NULL; |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 235 | int want_clusters = 0; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 236 | int want_meta = 0; |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 237 | int xattr_credits = 0; |
| 238 | struct ocfs2_security_xattr_info si = { |
| 239 | .enable = 1, |
| 240 | }; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 241 | int did_quota_inode = 0; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 242 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 243 | |
| 244 | mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode, |
| 245 | (unsigned long)dev, dentry->d_name.len, |
| 246 | dentry->d_name.name); |
| 247 | |
| 248 | /* get our super block */ |
| 249 | osb = OCFS2_SB(dir->i_sb); |
| 250 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 251 | status = ocfs2_inode_lock(dir, &parent_fe_bh, 1); |
Mark Fasheh | e3a8213 | 2006-10-05 16:04:17 -0700 | [diff] [blame] | 252 | if (status < 0) { |
| 253 | if (status != -ENOENT) |
| 254 | mlog_errno(status); |
| 255 | return status; |
| 256 | } |
| 257 | |
Mark Fasheh | a663e30 | 2006-08-09 11:45:07 -0700 | [diff] [blame] | 258 | if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) { |
| 259 | status = -EMLINK; |
| 260 | goto leave; |
| 261 | } |
| 262 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 263 | dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
| 264 | if (!dirfe->i_links_count) { |
| 265 | /* can't make a file in a deleted directory. */ |
| 266 | status = -ENOENT; |
| 267 | goto leave; |
| 268 | } |
| 269 | |
| 270 | status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, |
| 271 | dentry->d_name.len); |
| 272 | if (status) |
| 273 | goto leave; |
| 274 | |
| 275 | /* get a spot inside the dir. */ |
| 276 | status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, |
| 277 | dentry->d_name.name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 278 | dentry->d_name.len, &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 279 | if (status < 0) { |
| 280 | mlog_errno(status); |
| 281 | goto leave; |
| 282 | } |
| 283 | |
| 284 | /* reserve an inode spot */ |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 285 | status = ocfs2_reserve_new_inode(osb, &inode_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 286 | if (status < 0) { |
| 287 | if (status != -ENOSPC) |
| 288 | mlog_errno(status); |
| 289 | goto leave; |
| 290 | } |
| 291 | |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 292 | inode = ocfs2_get_init_inode(dir, mode); |
| 293 | if (!inode) { |
| 294 | status = -ENOMEM; |
| 295 | mlog_errno(status); |
| 296 | goto leave; |
| 297 | } |
| 298 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 299 | /* get security xattr */ |
| 300 | status = ocfs2_init_security_get(inode, dir, &si); |
| 301 | if (status) { |
| 302 | if (status == -EOPNOTSUPP) |
| 303 | si.enable = 0; |
| 304 | else { |
| 305 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 306 | goto leave; |
| 307 | } |
| 308 | } |
| 309 | |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 310 | /* calculate meta data/clusters for setting security and acl xattr */ |
| 311 | status = ocfs2_calc_xattr_init(dir, parent_fe_bh, mode, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 312 | &si, &want_clusters, |
| 313 | &xattr_credits, &want_meta); |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 314 | if (status < 0) { |
| 315 | mlog_errno(status); |
| 316 | goto leave; |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* Reserve a cluster if creating an extent based directory. */ |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 320 | if (S_ISDIR(mode) && !ocfs2_supports_inline_data(osb)) { |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 321 | want_clusters += 1; |
| 322 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 323 | /* Dir indexing requires extra space as well */ |
| 324 | if (ocfs2_supports_indexed_dirs(osb)) { |
| 325 | want_clusters++; |
| 326 | want_meta++; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | status = ocfs2_reserve_new_metadata_blocks(osb, want_meta, &meta_ac); |
| 331 | if (status < 0) { |
| 332 | if (status != -ENOSPC) |
| 333 | mlog_errno(status); |
| 334 | goto leave; |
| 335 | } |
| 336 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 337 | status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac); |
| 338 | if (status < 0) { |
| 339 | if (status != -ENOSPC) |
| 340 | mlog_errno(status); |
| 341 | goto leave; |
| 342 | } |
| 343 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 344 | handle = ocfs2_start_trans(osb, ocfs2_mknod_credits(osb->sb, |
| 345 | S_ISDIR(mode), |
| 346 | xattr_credits)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 347 | if (IS_ERR(handle)) { |
| 348 | status = PTR_ERR(handle); |
| 349 | handle = NULL; |
| 350 | mlog_errno(status); |
| 351 | goto leave; |
| 352 | } |
| 353 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 354 | /* We don't use standard VFS wrapper because we don't want vfs_dq_init |
| 355 | * to be called. */ |
| 356 | if (sb_any_quota_active(osb->sb) && |
| 357 | osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) { |
| 358 | status = -EDQUOT; |
| 359 | goto leave; |
| 360 | } |
| 361 | did_quota_inode = 1; |
| 362 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 363 | /* do the real work now. */ |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 364 | status = ocfs2_mknod_locked(osb, dir, inode, dentry, dev, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 365 | &new_fe_bh, parent_fe_bh, handle, |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 366 | inode_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 367 | if (status < 0) { |
| 368 | mlog_errno(status); |
| 369 | goto leave; |
| 370 | } |
| 371 | |
| 372 | if (S_ISDIR(mode)) { |
| 373 | status = ocfs2_fill_new_dir(osb, handle, dir, inode, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 374 | new_fe_bh, data_ac, meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 375 | if (status < 0) { |
| 376 | mlog_errno(status); |
| 377 | goto leave; |
| 378 | } |
| 379 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 380 | status = ocfs2_journal_access_di(handle, dir, parent_fe_bh, |
| 381 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 382 | if (status < 0) { |
| 383 | mlog_errno(status); |
| 384 | goto leave; |
| 385 | } |
| 386 | le16_add_cpu(&dirfe->i_links_count, 1); |
| 387 | status = ocfs2_journal_dirty(handle, parent_fe_bh); |
| 388 | if (status < 0) { |
| 389 | mlog_errno(status); |
| 390 | goto leave; |
| 391 | } |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 392 | inc_nlink(dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 395 | status = ocfs2_init_acl(handle, inode, dir, new_fe_bh, parent_fe_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 396 | meta_ac, data_ac); |
Tiger Yang | 89c38bd | 2008-11-14 11:17:41 +0800 | [diff] [blame] | 397 | if (status < 0) { |
| 398 | mlog_errno(status); |
| 399 | goto leave; |
| 400 | } |
| 401 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 402 | if (si.enable) { |
| 403 | status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 404 | meta_ac, data_ac); |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 405 | if (status < 0) { |
| 406 | mlog_errno(status); |
| 407 | goto leave; |
| 408 | } |
| 409 | } |
| 410 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 411 | status = ocfs2_add_entry(handle, dentry, inode, |
| 412 | OCFS2_I(inode)->ip_blkno, parent_fe_bh, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 413 | &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 414 | if (status < 0) { |
| 415 | mlog_errno(status); |
| 416 | goto leave; |
| 417 | } |
| 418 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 419 | status = ocfs2_dentry_attach_lock(dentry, inode, |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 420 | OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 421 | if (status) { |
| 422 | mlog_errno(status); |
| 423 | goto leave; |
| 424 | } |
| 425 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 426 | insert_inode_hash(inode); |
| 427 | dentry->d_op = &ocfs2_dentry_ops; |
| 428 | d_instantiate(dentry, inode); |
| 429 | status = 0; |
| 430 | leave: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 431 | if (status < 0 && did_quota_inode) |
| 432 | vfs_dq_free_inode(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 433 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 434 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 435 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 436 | ocfs2_inode_unlock(dir, 1); |
Mark Fasheh | e3a8213 | 2006-10-05 16:04:17 -0700 | [diff] [blame] | 437 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 438 | if (status == -ENOSPC) |
| 439 | mlog(0, "Disk is full\n"); |
| 440 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 441 | brelse(new_fe_bh); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 442 | brelse(parent_fe_bh); |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 443 | kfree(si.name); |
| 444 | kfree(si.value); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 445 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 446 | ocfs2_free_dir_lookup_result(&lookup); |
| 447 | |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 448 | if ((status < 0) && inode) { |
| 449 | clear_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 450 | iput(inode); |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 451 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 452 | |
| 453 | if (inode_ac) |
| 454 | ocfs2_free_alloc_context(inode_ac); |
| 455 | |
| 456 | if (data_ac) |
| 457 | ocfs2_free_alloc_context(data_ac); |
| 458 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 459 | if (meta_ac) |
| 460 | ocfs2_free_alloc_context(meta_ac); |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 461 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 462 | mlog_exit(status); |
| 463 | |
| 464 | return status; |
| 465 | } |
| 466 | |
| 467 | static int ocfs2_mknod_locked(struct ocfs2_super *osb, |
| 468 | struct inode *dir, |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 469 | struct inode *inode, |
| 470 | struct dentry *dentry, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 471 | dev_t dev, |
| 472 | struct buffer_head **new_fe_bh, |
| 473 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 474 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 475 | struct ocfs2_alloc_context *inode_ac) |
| 476 | { |
| 477 | int status = 0; |
| 478 | struct ocfs2_dinode *fe = NULL; |
| 479 | struct ocfs2_extent_list *fel; |
| 480 | u64 fe_blkno = 0; |
| 481 | u16 suballoc_bit; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 482 | u16 feat; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 483 | |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 484 | mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, |
| 485 | inode->i_mode, (unsigned long)dev, dentry->d_name.len, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 486 | dentry->d_name.name); |
| 487 | |
| 488 | *new_fe_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 489 | |
| 490 | status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit, |
| 491 | &fe_blkno); |
| 492 | if (status < 0) { |
| 493 | mlog_errno(status); |
| 494 | goto leave; |
| 495 | } |
| 496 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 497 | /* populate as many fields early on as possible - many of |
| 498 | * these are used by the support functions here and in |
| 499 | * callers. */ |
| 500 | inode->i_ino = ino_from_blkno(osb->sb, fe_blkno); |
| 501 | OCFS2_I(inode)->ip_blkno = fe_blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 502 | spin_lock(&osb->osb_lock); |
| 503 | inode->i_generation = osb->s_next_generation++; |
| 504 | spin_unlock(&osb->osb_lock); |
| 505 | |
| 506 | *new_fe_bh = sb_getblk(osb->sb, fe_blkno); |
| 507 | if (!*new_fe_bh) { |
| 508 | status = -EIO; |
| 509 | mlog_errno(status); |
| 510 | goto leave; |
| 511 | } |
| 512 | ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh); |
| 513 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 514 | status = ocfs2_journal_access_di(handle, inode, *new_fe_bh, |
| 515 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 516 | if (status < 0) { |
| 517 | mlog_errno(status); |
| 518 | goto leave; |
| 519 | } |
| 520 | |
| 521 | fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data; |
| 522 | memset(fe, 0, osb->sb->s_blocksize); |
| 523 | |
| 524 | fe->i_generation = cpu_to_le32(inode->i_generation); |
| 525 | fe->i_fs_generation = cpu_to_le32(osb->fs_generation); |
| 526 | fe->i_blkno = cpu_to_le64(fe_blkno); |
| 527 | fe->i_suballoc_bit = cpu_to_le16(suballoc_bit); |
Tao Ma | 4d0ddb2 | 2008-03-05 16:11:46 +0800 | [diff] [blame] | 528 | fe->i_suballoc_slot = cpu_to_le16(inode_ac->ac_alloc_slot); |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 529 | fe->i_uid = cpu_to_le32(inode->i_uid); |
| 530 | fe->i_gid = cpu_to_le32(inode->i_gid); |
| 531 | fe->i_mode = cpu_to_le16(inode->i_mode); |
| 532 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 533 | fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 534 | fe->i_links_count = cpu_to_le16(inode->i_nlink); |
| 535 | |
| 536 | fe->i_last_eb_blk = 0; |
| 537 | strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE); |
| 538 | le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL); |
| 539 | fe->i_atime = fe->i_ctime = fe->i_mtime = |
| 540 | cpu_to_le64(CURRENT_TIME.tv_sec); |
| 541 | fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec = |
| 542 | cpu_to_le32(CURRENT_TIME.tv_nsec); |
| 543 | fe->i_dtime = 0; |
| 544 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 545 | /* |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 546 | * If supported, directories start with inline data. If inline |
| 547 | * isn't supported, but indexing is, we start them as indexed. |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 548 | */ |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame^] | 549 | feat = le16_to_cpu(fe->i_dyn_features); |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 550 | if (S_ISDIR(inode->i_mode) && ocfs2_supports_inline_data(osb)) { |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 551 | fe->i_dyn_features = cpu_to_le16(feat | OCFS2_INLINE_DATA_FL); |
| 552 | |
Tiger Yang | d9ae49d | 2009-03-05 11:06:15 +0800 | [diff] [blame] | 553 | fe->id2.i_data.id_count = cpu_to_le16( |
| 554 | ocfs2_max_inline_data_with_xattr(osb->sb, fe)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 555 | } else { |
| 556 | fel = &fe->id2.i_list; |
| 557 | fel->l_tree_depth = 0; |
| 558 | fel->l_next_free_rec = 0; |
| 559 | fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb)); |
| 560 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 561 | |
| 562 | status = ocfs2_journal_dirty(handle, *new_fe_bh); |
| 563 | if (status < 0) { |
| 564 | mlog_errno(status); |
| 565 | goto leave; |
| 566 | } |
| 567 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 568 | ocfs2_populate_inode(inode, fe, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 569 | ocfs2_inode_set_new(osb, inode); |
Sunil Mushran | c271c5c | 2006-12-05 17:56:35 -0800 | [diff] [blame] | 570 | if (!ocfs2_mount_local(osb)) { |
| 571 | status = ocfs2_create_new_inode_locks(inode); |
| 572 | if (status < 0) |
| 573 | mlog_errno(status); |
| 574 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 575 | |
| 576 | status = 0; /* error in ocfs2_create_new_inode_locks is not |
| 577 | * critical */ |
| 578 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 579 | leave: |
| 580 | if (status < 0) { |
| 581 | if (*new_fe_bh) { |
| 582 | brelse(*new_fe_bh); |
| 583 | *new_fe_bh = NULL; |
| 584 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | mlog_exit(status); |
| 588 | return status; |
| 589 | } |
| 590 | |
| 591 | static int ocfs2_mkdir(struct inode *dir, |
| 592 | struct dentry *dentry, |
| 593 | int mode) |
| 594 | { |
| 595 | int ret; |
| 596 | |
| 597 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode, |
| 598 | dentry->d_name.len, dentry->d_name.name); |
| 599 | ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0); |
| 600 | mlog_exit(ret); |
| 601 | |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | static int ocfs2_create(struct inode *dir, |
| 606 | struct dentry *dentry, |
| 607 | int mode, |
| 608 | struct nameidata *nd) |
| 609 | { |
| 610 | int ret; |
| 611 | |
| 612 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode, |
| 613 | dentry->d_name.len, dentry->d_name.name); |
| 614 | ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0); |
| 615 | mlog_exit(ret); |
| 616 | |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | static int ocfs2_link(struct dentry *old_dentry, |
| 621 | struct inode *dir, |
| 622 | struct dentry *dentry) |
| 623 | { |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 624 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 625 | struct inode *inode = old_dentry->d_inode; |
| 626 | int err; |
| 627 | struct buffer_head *fe_bh = NULL; |
| 628 | struct buffer_head *parent_fe_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 629 | struct ocfs2_dinode *fe = NULL; |
| 630 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 631 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 632 | |
| 633 | mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino, |
| 634 | old_dentry->d_name.len, old_dentry->d_name.name, |
| 635 | dentry->d_name.len, dentry->d_name.name); |
| 636 | |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 637 | if (S_ISDIR(inode->i_mode)) |
| 638 | return -EPERM; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 639 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 640 | err = ocfs2_inode_lock(dir, &parent_fe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 641 | if (err < 0) { |
| 642 | if (err != -ENOENT) |
| 643 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 644 | return err; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Tiger Yang | 0f62de2 | 2006-08-31 20:39:47 -0700 | [diff] [blame] | 647 | if (!dir->i_nlink) { |
| 648 | err = -ENOENT; |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 649 | goto out; |
Tiger Yang | 0f62de2 | 2006-08-31 20:39:47 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 652 | err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, |
| 653 | dentry->d_name.len); |
| 654 | if (err) |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 655 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 656 | |
| 657 | err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, |
| 658 | dentry->d_name.name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 659 | dentry->d_name.len, &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 660 | if (err < 0) { |
| 661 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 662 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 665 | err = ocfs2_inode_lock(inode, &fe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 666 | if (err < 0) { |
| 667 | if (err != -ENOENT) |
| 668 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 669 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 673 | if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) { |
| 674 | err = -EMLINK; |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 675 | goto out_unlock_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 678 | handle = ocfs2_start_trans(osb, ocfs2_link_credits(osb->sb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 679 | if (IS_ERR(handle)) { |
| 680 | err = PTR_ERR(handle); |
| 681 | handle = NULL; |
| 682 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 683 | goto out_unlock_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 686 | err = ocfs2_journal_access_di(handle, inode, fe_bh, |
| 687 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 688 | if (err < 0) { |
| 689 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 690 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 693 | inc_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 694 | inode->i_ctime = CURRENT_TIME; |
| 695 | fe->i_links_count = cpu_to_le16(inode->i_nlink); |
| 696 | fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 697 | fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 698 | |
| 699 | err = ocfs2_journal_dirty(handle, fe_bh); |
| 700 | if (err < 0) { |
| 701 | le16_add_cpu(&fe->i_links_count, -1); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 702 | drop_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 703 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 704 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | err = ocfs2_add_entry(handle, dentry, inode, |
| 708 | OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 709 | parent_fe_bh, &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 710 | if (err) { |
| 711 | le16_add_cpu(&fe->i_links_count, -1); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 712 | drop_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 713 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 714 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 717 | err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 718 | if (err) { |
| 719 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 720 | goto out_commit; |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 723 | atomic_inc(&inode->i_count); |
| 724 | dentry->d_op = &ocfs2_dentry_ops; |
| 725 | d_instantiate(dentry, inode); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 726 | |
| 727 | out_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 728 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 729 | out_unlock_inode: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 730 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 731 | |
| 732 | out: |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 733 | ocfs2_inode_unlock(dir, 1); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 734 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 735 | brelse(fe_bh); |
| 736 | brelse(parent_fe_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 737 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 738 | ocfs2_free_dir_lookup_result(&lookup); |
| 739 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 740 | mlog_exit(err); |
| 741 | |
| 742 | return err; |
| 743 | } |
| 744 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 745 | /* |
| 746 | * Takes and drops an exclusive lock on the given dentry. This will |
| 747 | * force other nodes to drop it. |
| 748 | */ |
| 749 | static int ocfs2_remote_dentry_delete(struct dentry *dentry) |
| 750 | { |
| 751 | int ret; |
| 752 | |
| 753 | ret = ocfs2_dentry_lock(dentry, 1); |
| 754 | if (ret) |
| 755 | mlog_errno(ret); |
| 756 | else |
| 757 | ocfs2_dentry_unlock(dentry, 1); |
| 758 | |
| 759 | return ret; |
| 760 | } |
| 761 | |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 762 | static inline int inode_is_unlinkable(struct inode *inode) |
| 763 | { |
| 764 | if (S_ISDIR(inode->i_mode)) { |
| 765 | if (inode->i_nlink == 2) |
| 766 | return 1; |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | if (inode->i_nlink == 1) |
| 771 | return 1; |
| 772 | return 0; |
| 773 | } |
| 774 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 775 | static int ocfs2_unlink(struct inode *dir, |
| 776 | struct dentry *dentry) |
| 777 | { |
| 778 | int status; |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 779 | int child_locked = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 780 | struct inode *inode = dentry->d_inode; |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 781 | struct inode *orphan_dir = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 782 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 783 | u64 blkno; |
| 784 | struct ocfs2_dinode *fe = NULL; |
| 785 | struct buffer_head *fe_bh = NULL; |
| 786 | struct buffer_head *parent_node_bh = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 787 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 788 | char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 789 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
| 790 | struct ocfs2_dir_lookup_result orphan_insert = { NULL, }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 791 | |
| 792 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry, |
| 793 | dentry->d_name.len, dentry->d_name.name); |
| 794 | |
| 795 | BUG_ON(dentry->d_parent->d_inode != dir); |
| 796 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 797 | mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 798 | |
| 799 | if (inode == osb->root_inode) { |
| 800 | mlog(0, "Cannot delete the root directory\n"); |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 801 | return -EPERM; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 804 | status = ocfs2_inode_lock(dir, &parent_node_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 805 | if (status < 0) { |
| 806 | if (status != -ENOENT) |
| 807 | mlog_errno(status); |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 808 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | status = ocfs2_find_files_on_disk(dentry->d_name.name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 812 | dentry->d_name.len, &blkno, dir, |
| 813 | &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 814 | if (status < 0) { |
| 815 | if (status != -ENOENT) |
| 816 | mlog_errno(status); |
| 817 | goto leave; |
| 818 | } |
| 819 | |
| 820 | if (OCFS2_I(inode)->ip_blkno != blkno) { |
| 821 | status = -ENOENT; |
| 822 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 823 | mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n", |
| 824 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 825 | (unsigned long long)blkno, OCFS2_I(inode)->ip_flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 826 | goto leave; |
| 827 | } |
| 828 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 829 | status = ocfs2_inode_lock(inode, &fe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 830 | if (status < 0) { |
| 831 | if (status != -ENOENT) |
| 832 | mlog_errno(status); |
| 833 | goto leave; |
| 834 | } |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 835 | child_locked = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 836 | |
| 837 | if (S_ISDIR(inode->i_mode)) { |
| 838 | if (!ocfs2_empty_dir(inode)) { |
| 839 | status = -ENOTEMPTY; |
| 840 | goto leave; |
| 841 | } else if (inode->i_nlink != 2) { |
| 842 | status = -ENOTEMPTY; |
| 843 | goto leave; |
| 844 | } |
| 845 | } |
| 846 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 847 | status = ocfs2_remote_dentry_delete(dentry); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 848 | if (status < 0) { |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 849 | /* This remote delete should succeed under all normal |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 850 | * circumstances. */ |
| 851 | mlog_errno(status); |
| 852 | goto leave; |
| 853 | } |
| 854 | |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 855 | if (inode_is_unlinkable(inode)) { |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 856 | status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 857 | orphan_name, &orphan_insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 858 | if (status < 0) { |
| 859 | mlog_errno(status); |
| 860 | goto leave; |
| 861 | } |
| 862 | } |
| 863 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 864 | handle = ocfs2_start_trans(osb, ocfs2_unlink_credits(osb->sb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 865 | if (IS_ERR(handle)) { |
| 866 | status = PTR_ERR(handle); |
| 867 | handle = NULL; |
| 868 | mlog_errno(status); |
| 869 | goto leave; |
| 870 | } |
| 871 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 872 | status = ocfs2_journal_access_di(handle, inode, fe_bh, |
| 873 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 874 | if (status < 0) { |
| 875 | mlog_errno(status); |
| 876 | goto leave; |
| 877 | } |
| 878 | |
| 879 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 880 | |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 881 | if (inode_is_unlinkable(inode)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 882 | status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 883 | &orphan_insert, orphan_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 884 | if (status < 0) { |
| 885 | mlog_errno(status); |
| 886 | goto leave; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* delete the name from the parent dir */ |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 891 | status = ocfs2_delete_entry(handle, dir, &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 892 | if (status < 0) { |
| 893 | mlog_errno(status); |
| 894 | goto leave; |
| 895 | } |
| 896 | |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 897 | if (S_ISDIR(inode->i_mode)) |
| 898 | drop_nlink(inode); |
| 899 | drop_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 900 | fe->i_links_count = cpu_to_le16(inode->i_nlink); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 901 | |
| 902 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 903 | if (status < 0) { |
| 904 | mlog_errno(status); |
| 905 | goto leave; |
| 906 | } |
| 907 | |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 908 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 909 | if (S_ISDIR(inode->i_mode)) |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 910 | drop_nlink(dir); |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 911 | |
| 912 | status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh); |
| 913 | if (status < 0) { |
| 914 | mlog_errno(status); |
| 915 | if (S_ISDIR(inode->i_mode)) |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 916 | inc_nlink(dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | leave: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 920 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 921 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 922 | |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 923 | if (child_locked) |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 924 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 925 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 926 | ocfs2_inode_unlock(dir, 1); |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 927 | |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 928 | if (orphan_dir) { |
| 929 | /* This was locked for us in ocfs2_prepare_orphan_dir() */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 930 | ocfs2_inode_unlock(orphan_dir, 1); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 931 | mutex_unlock(&orphan_dir->i_mutex); |
| 932 | iput(orphan_dir); |
| 933 | } |
| 934 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 935 | brelse(fe_bh); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 936 | brelse(parent_node_bh); |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 937 | |
| 938 | ocfs2_free_dir_lookup_result(&orphan_insert); |
| 939 | ocfs2_free_dir_lookup_result(&lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 940 | |
| 941 | mlog_exit(status); |
| 942 | |
| 943 | return status; |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * The only place this should be used is rename! |
| 948 | * if they have the same id, then the 1st one is the only one locked. |
| 949 | */ |
| 950 | static int ocfs2_double_lock(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 951 | struct buffer_head **bh1, |
| 952 | struct inode *inode1, |
| 953 | struct buffer_head **bh2, |
| 954 | struct inode *inode2) |
| 955 | { |
| 956 | int status; |
| 957 | struct ocfs2_inode_info *oi1 = OCFS2_I(inode1); |
| 958 | struct ocfs2_inode_info *oi2 = OCFS2_I(inode2); |
| 959 | struct buffer_head **tmpbh; |
| 960 | struct inode *tmpinode; |
| 961 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 962 | mlog_entry("(inode1 = %llu, inode2 = %llu)\n", |
| 963 | (unsigned long long)oi1->ip_blkno, |
| 964 | (unsigned long long)oi2->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 965 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 966 | if (*bh1) |
| 967 | *bh1 = NULL; |
| 968 | if (*bh2) |
| 969 | *bh2 = NULL; |
| 970 | |
| 971 | /* we always want to lock the one with the lower lockid first. */ |
| 972 | if (oi1->ip_blkno != oi2->ip_blkno) { |
| 973 | if (oi1->ip_blkno < oi2->ip_blkno) { |
| 974 | /* switch id1 and id2 around */ |
| 975 | mlog(0, "switching them around...\n"); |
| 976 | tmpbh = bh2; |
| 977 | bh2 = bh1; |
| 978 | bh1 = tmpbh; |
| 979 | |
| 980 | tmpinode = inode2; |
| 981 | inode2 = inode1; |
| 982 | inode1 = tmpinode; |
| 983 | } |
| 984 | /* lock id2 */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 985 | status = ocfs2_inode_lock(inode2, bh2, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 986 | if (status < 0) { |
| 987 | if (status != -ENOENT) |
| 988 | mlog_errno(status); |
| 989 | goto bail; |
| 990 | } |
| 991 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 992 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 993 | /* lock id1 */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 994 | status = ocfs2_inode_lock(inode1, bh1, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 995 | if (status < 0) { |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 996 | /* |
| 997 | * An error return must mean that no cluster locks |
| 998 | * were held on function exit. |
| 999 | */ |
| 1000 | if (oi1->ip_blkno != oi2->ip_blkno) |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1001 | ocfs2_inode_unlock(inode2, 1); |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1002 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1003 | if (status != -ENOENT) |
| 1004 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1005 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1006 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1007 | bail: |
| 1008 | mlog_exit(status); |
| 1009 | return status; |
| 1010 | } |
| 1011 | |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1012 | static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2) |
| 1013 | { |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1014 | ocfs2_inode_unlock(inode1, 1); |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1015 | |
| 1016 | if (inode1 != inode2) |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1017 | ocfs2_inode_unlock(inode2, 1); |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1020 | static int ocfs2_rename(struct inode *old_dir, |
| 1021 | struct dentry *old_dentry, |
| 1022 | struct inode *new_dir, |
| 1023 | struct dentry *new_dentry) |
| 1024 | { |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1025 | int status = 0, rename_lock = 0, parents_locked = 0, target_exists = 0; |
| 1026 | int old_child_locked = 0, new_child_locked = 0, update_dot_dot = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1027 | struct inode *old_inode = old_dentry->d_inode; |
| 1028 | struct inode *new_inode = new_dentry->d_inode; |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1029 | struct inode *orphan_dir = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1030 | struct ocfs2_dinode *newfe = NULL; |
| 1031 | char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; |
| 1032 | struct buffer_head *orphan_entry_bh = NULL; |
| 1033 | struct buffer_head *newfe_bh = NULL; |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1034 | struct buffer_head *old_inode_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1035 | struct buffer_head *insert_entry_bh = NULL; |
| 1036 | struct ocfs2_super *osb = NULL; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1037 | u64 newfe_blkno, old_de_ino; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1038 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1039 | struct buffer_head *old_dir_bh = NULL; |
| 1040 | struct buffer_head *new_dir_bh = NULL; |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1041 | nlink_t old_dir_nlink = old_dir->i_nlink; |
Sunil Mushran | 480214d | 2007-08-06 15:11:56 -0700 | [diff] [blame] | 1042 | struct ocfs2_dinode *old_di; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1043 | struct ocfs2_dir_lookup_result old_inode_dot_dot_res = { NULL, }; |
| 1044 | struct ocfs2_dir_lookup_result target_lookup_res = { NULL, }; |
| 1045 | struct ocfs2_dir_lookup_result old_entry_lookup = { NULL, }; |
| 1046 | struct ocfs2_dir_lookup_result orphan_insert = { NULL, }; |
| 1047 | struct ocfs2_dir_lookup_result target_insert = { NULL, }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1048 | |
| 1049 | /* At some point it might be nice to break this function up a |
| 1050 | * bit. */ |
| 1051 | |
| 1052 | mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n", |
| 1053 | old_dir, old_dentry, new_dir, new_dentry, |
| 1054 | old_dentry->d_name.len, old_dentry->d_name.name, |
| 1055 | new_dentry->d_name.len, new_dentry->d_name.name); |
| 1056 | |
| 1057 | osb = OCFS2_SB(old_dir->i_sb); |
| 1058 | |
| 1059 | if (new_inode) { |
| 1060 | if (!igrab(new_inode)) |
| 1061 | BUG(); |
| 1062 | } |
| 1063 | |
Uwe Kleine-König | 1b3c371 | 2007-02-17 19:23:03 +0100 | [diff] [blame] | 1064 | /* Assume a directory hierarchy thusly: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1065 | * a/b/c |
| 1066 | * a/d |
| 1067 | * a,b,c, and d are all directories. |
| 1068 | * |
| 1069 | * from cwd of 'a' on both nodes: |
| 1070 | * node1: mv b/c d |
| 1071 | * node2: mv d b/c |
| 1072 | * |
| 1073 | * And that's why, just like the VFS, we need a file system |
| 1074 | * rename lock. */ |
Jan Kara | 5dabd69 | 2008-02-21 18:00:00 +0100 | [diff] [blame] | 1075 | if (old_dir != new_dir && S_ISDIR(old_inode->i_mode)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1076 | status = ocfs2_rename_lock(osb); |
| 1077 | if (status < 0) { |
| 1078 | mlog_errno(status); |
| 1079 | goto bail; |
| 1080 | } |
| 1081 | rename_lock = 1; |
| 1082 | } |
| 1083 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1084 | /* if old and new are the same, this'll just do one lock. */ |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1085 | status = ocfs2_double_lock(osb, &old_dir_bh, old_dir, |
| 1086 | &new_dir_bh, new_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1087 | if (status < 0) { |
| 1088 | mlog_errno(status); |
| 1089 | goto bail; |
| 1090 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1091 | parents_locked = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1092 | |
| 1093 | /* make sure both dirs have bhs |
| 1094 | * get an extra ref on old_dir_bh if old==new */ |
| 1095 | if (!new_dir_bh) { |
| 1096 | if (old_dir_bh) { |
| 1097 | new_dir_bh = old_dir_bh; |
| 1098 | get_bh(new_dir_bh); |
| 1099 | } else { |
| 1100 | mlog(ML_ERROR, "no old_dir_bh!\n"); |
| 1101 | status = -EIO; |
| 1102 | goto bail; |
| 1103 | } |
| 1104 | } |
| 1105 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1106 | /* |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1107 | * Aside from allowing a meta data update, the locking here |
Mark Fasheh | 34d024f | 2007-09-24 15:56:19 -0700 | [diff] [blame] | 1108 | * also ensures that the downconvert thread on other nodes |
| 1109 | * won't have to concurrently downconvert the inode and the |
| 1110 | * dentry locks. |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1111 | */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1112 | status = ocfs2_inode_lock(old_inode, &old_inode_bh, 1); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1113 | if (status < 0) { |
| 1114 | if (status != -ENOENT) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1115 | mlog_errno(status); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1116 | goto bail; |
| 1117 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1118 | old_child_locked = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1119 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1120 | status = ocfs2_remote_dentry_delete(old_dentry); |
| 1121 | if (status < 0) { |
| 1122 | mlog_errno(status); |
| 1123 | goto bail; |
| 1124 | } |
| 1125 | |
| 1126 | if (S_ISDIR(old_inode->i_mode)) { |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1127 | u64 old_inode_parent; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1128 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1129 | update_dot_dot = 1; |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1130 | status = ocfs2_find_files_on_disk("..", 2, &old_inode_parent, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1131 | old_inode, |
| 1132 | &old_inode_dot_dot_res); |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1133 | if (status) { |
| 1134 | status = -EIO; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1135 | goto bail; |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | if (old_inode_parent != OCFS2_I(old_dir)->ip_blkno) { |
| 1139 | status = -EIO; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1140 | goto bail; |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | if (!new_inode && new_dir != old_dir && |
| 1144 | new_dir->i_nlink >= OCFS2_LINK_MAX) { |
| 1145 | status = -EMLINK; |
| 1146 | goto bail; |
| 1147 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1150 | status = ocfs2_lookup_ino_from_name(old_dir, old_dentry->d_name.name, |
| 1151 | old_dentry->d_name.len, |
| 1152 | &old_de_ino); |
| 1153 | if (status) { |
| 1154 | status = -ENOENT; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1155 | goto bail; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1156 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1157 | |
| 1158 | /* |
| 1159 | * Check for inode number is _not_ due to possible IO errors. |
| 1160 | * We might rmdir the source, keep it as pwd of some process |
| 1161 | * and merrily kill the link to whatever was created under the |
| 1162 | * same name. Goodbye sticky bit ;-< |
| 1163 | */ |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1164 | if (old_de_ino != OCFS2_I(old_inode)->ip_blkno) { |
| 1165 | status = -ENOENT; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1166 | goto bail; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1167 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1168 | |
| 1169 | /* check if the target already exists (in which case we need |
| 1170 | * to delete it */ |
| 1171 | status = ocfs2_find_files_on_disk(new_dentry->d_name.name, |
| 1172 | new_dentry->d_name.len, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1173 | &newfe_blkno, new_dir, |
| 1174 | &target_lookup_res); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1175 | /* The only error we allow here is -ENOENT because the new |
| 1176 | * file not existing is perfectly valid. */ |
| 1177 | if ((status < 0) && (status != -ENOENT)) { |
| 1178 | /* If we cannot find the file specified we should just */ |
| 1179 | /* return the error... */ |
| 1180 | mlog_errno(status); |
| 1181 | goto bail; |
| 1182 | } |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1183 | if (status == 0) |
| 1184 | target_exists = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1185 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1186 | if (!target_exists && new_inode) { |
Srinivas Eeda | e325a88 | 2007-10-31 16:49:43 -0700 | [diff] [blame] | 1187 | /* |
| 1188 | * Target was unlinked by another node while we were |
| 1189 | * waiting to get to ocfs2_rename(). There isn't |
| 1190 | * anything we can do here to help the situation, so |
| 1191 | * bubble up the appropriate error. |
| 1192 | */ |
| 1193 | status = -ENOENT; |
| 1194 | goto bail; |
| 1195 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1196 | |
| 1197 | /* In case we need to overwrite an existing file, we blow it |
| 1198 | * away first */ |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1199 | if (target_exists) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1200 | /* VFS didn't think there existed an inode here, but |
| 1201 | * someone else in the cluster must have raced our |
| 1202 | * rename to create one. Today we error cleanly, in |
| 1203 | * the future we should consider calling iget to build |
| 1204 | * a new struct inode for this entry. */ |
| 1205 | if (!new_inode) { |
| 1206 | status = -EACCES; |
| 1207 | |
| 1208 | mlog(0, "We found an inode for name %.*s but VFS " |
| 1209 | "didn't give us one.\n", new_dentry->d_name.len, |
| 1210 | new_dentry->d_name.name); |
| 1211 | goto bail; |
| 1212 | } |
| 1213 | |
| 1214 | if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) { |
| 1215 | status = -EACCES; |
| 1216 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1217 | mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n", |
| 1218 | (unsigned long long)OCFS2_I(new_inode)->ip_blkno, |
| 1219 | (unsigned long long)newfe_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1220 | OCFS2_I(new_inode)->ip_flags); |
| 1221 | goto bail; |
| 1222 | } |
| 1223 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1224 | status = ocfs2_inode_lock(new_inode, &newfe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1225 | if (status < 0) { |
| 1226 | if (status != -ENOENT) |
| 1227 | mlog_errno(status); |
| 1228 | goto bail; |
| 1229 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1230 | new_child_locked = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1231 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1232 | status = ocfs2_remote_dentry_delete(new_dentry); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1233 | if (status < 0) { |
| 1234 | mlog_errno(status); |
| 1235 | goto bail; |
| 1236 | } |
| 1237 | |
| 1238 | newfe = (struct ocfs2_dinode *) newfe_bh->b_data; |
| 1239 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1240 | mlog(0, "aha rename over existing... new_blkno=%llu " |
| 1241 | "newfebh=%p bhblocknr=%llu\n", |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1242 | (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ? |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1243 | (unsigned long long)newfe_bh->b_blocknr : 0ULL); |
| 1244 | |
| 1245 | if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) { |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1246 | status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1247 | new_inode, |
| 1248 | orphan_name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1249 | &orphan_insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1250 | if (status < 0) { |
| 1251 | mlog_errno(status); |
| 1252 | goto bail; |
| 1253 | } |
| 1254 | } |
| 1255 | } else { |
| 1256 | BUG_ON(new_dentry->d_parent->d_inode != new_dir); |
| 1257 | |
| 1258 | status = ocfs2_check_dir_for_entry(new_dir, |
| 1259 | new_dentry->d_name.name, |
| 1260 | new_dentry->d_name.len); |
| 1261 | if (status) |
| 1262 | goto bail; |
| 1263 | |
| 1264 | status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh, |
| 1265 | new_dentry->d_name.name, |
| 1266 | new_dentry->d_name.len, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1267 | &target_insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1268 | if (status < 0) { |
| 1269 | mlog_errno(status); |
| 1270 | goto bail; |
| 1271 | } |
| 1272 | } |
| 1273 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1274 | handle = ocfs2_start_trans(osb, ocfs2_rename_credits(osb->sb)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1275 | if (IS_ERR(handle)) { |
| 1276 | status = PTR_ERR(handle); |
| 1277 | handle = NULL; |
| 1278 | mlog_errno(status); |
| 1279 | goto bail; |
| 1280 | } |
| 1281 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1282 | if (target_exists) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1283 | if (S_ISDIR(new_inode->i_mode)) { |
| 1284 | if (!ocfs2_empty_dir(new_inode) || |
| 1285 | new_inode->i_nlink != 2) { |
| 1286 | status = -ENOTEMPTY; |
| 1287 | goto bail; |
| 1288 | } |
| 1289 | } |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1290 | status = ocfs2_journal_access_di(handle, new_inode, newfe_bh, |
| 1291 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1292 | if (status < 0) { |
| 1293 | mlog_errno(status); |
| 1294 | goto bail; |
| 1295 | } |
| 1296 | |
| 1297 | if (S_ISDIR(new_inode->i_mode) || |
| 1298 | (newfe->i_links_count == cpu_to_le16(1))){ |
| 1299 | status = ocfs2_orphan_add(osb, handle, new_inode, |
| 1300 | newfe, orphan_name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1301 | &orphan_insert, orphan_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1302 | if (status < 0) { |
| 1303 | mlog_errno(status); |
| 1304 | goto bail; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | /* change the dirent to point to the correct inode */ |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1309 | status = ocfs2_update_entry(new_dir, handle, &target_lookup_res, |
| 1310 | old_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1311 | if (status < 0) { |
| 1312 | mlog_errno(status); |
| 1313 | goto bail; |
| 1314 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1315 | new_dir->i_version++; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1316 | |
| 1317 | if (S_ISDIR(new_inode->i_mode)) |
| 1318 | newfe->i_links_count = 0; |
| 1319 | else |
| 1320 | le16_add_cpu(&newfe->i_links_count, -1); |
| 1321 | |
| 1322 | status = ocfs2_journal_dirty(handle, newfe_bh); |
| 1323 | if (status < 0) { |
| 1324 | mlog_errno(status); |
| 1325 | goto bail; |
| 1326 | } |
| 1327 | } else { |
| 1328 | /* if the name was not found in new_dir, add it now */ |
| 1329 | status = ocfs2_add_entry(handle, new_dentry, old_inode, |
| 1330 | OCFS2_I(old_inode)->ip_blkno, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1331 | new_dir_bh, &target_insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | old_inode->i_ctime = CURRENT_TIME; |
| 1335 | mark_inode_dirty(old_inode); |
Sunil Mushran | 480214d | 2007-08-06 15:11:56 -0700 | [diff] [blame] | 1336 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1337 | status = ocfs2_journal_access_di(handle, old_inode, old_inode_bh, |
| 1338 | OCFS2_JOURNAL_ACCESS_WRITE); |
Sunil Mushran | 480214d | 2007-08-06 15:11:56 -0700 | [diff] [blame] | 1339 | if (status >= 0) { |
| 1340 | old_di = (struct ocfs2_dinode *) old_inode_bh->b_data; |
| 1341 | |
| 1342 | old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec); |
| 1343 | old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec); |
| 1344 | |
| 1345 | status = ocfs2_journal_dirty(handle, old_inode_bh); |
| 1346 | if (status < 0) |
| 1347 | mlog_errno(status); |
| 1348 | } else |
| 1349 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1350 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1351 | /* |
| 1352 | * Now that the name has been added to new_dir, remove the old name. |
| 1353 | * |
| 1354 | * We don't keep any directory entry context around until now |
| 1355 | * because the insert might have changed the type of directory |
| 1356 | * we're dealing with. |
| 1357 | */ |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1358 | status = ocfs2_find_entry(old_dentry->d_name.name, |
| 1359 | old_dentry->d_name.len, old_dir, |
| 1360 | &old_entry_lookup); |
| 1361 | if (status) |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1362 | goto bail; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1363 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1364 | status = ocfs2_delete_entry(handle, old_dir, &old_entry_lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1365 | if (status < 0) { |
| 1366 | mlog_errno(status); |
| 1367 | goto bail; |
| 1368 | } |
| 1369 | |
| 1370 | if (new_inode) { |
| 1371 | new_inode->i_nlink--; |
| 1372 | new_inode->i_ctime = CURRENT_TIME; |
| 1373 | } |
| 1374 | old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1375 | |
| 1376 | if (update_dot_dot) { |
| 1377 | status = ocfs2_update_entry(old_inode, handle, |
| 1378 | &old_inode_dot_dot_res, new_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1379 | old_dir->i_nlink--; |
| 1380 | if (new_inode) { |
| 1381 | new_inode->i_nlink--; |
| 1382 | } else { |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1383 | inc_nlink(new_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1384 | mark_inode_dirty(new_dir); |
| 1385 | } |
| 1386 | } |
| 1387 | mark_inode_dirty(old_dir); |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1388 | ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh); |
| 1389 | if (new_inode) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1390 | mark_inode_dirty(new_inode); |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1391 | ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh); |
| 1392 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1393 | |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1394 | if (old_dir != new_dir) { |
| 1395 | /* Keep the same times on both directories.*/ |
| 1396 | new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime; |
| 1397 | |
| 1398 | /* |
| 1399 | * This will also pick up the i_nlink change from the |
| 1400 | * block above. |
| 1401 | */ |
| 1402 | ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh); |
| 1403 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1404 | |
| 1405 | if (old_dir_nlink != old_dir->i_nlink) { |
| 1406 | if (!old_dir_bh) { |
| 1407 | mlog(ML_ERROR, "need to change nlink for old dir " |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1408 | "%llu from %d to %d but bh is NULL!\n", |
| 1409 | (unsigned long long)OCFS2_I(old_dir)->ip_blkno, |
| 1410 | (int)old_dir_nlink, old_dir->i_nlink); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1411 | } else { |
| 1412 | struct ocfs2_dinode *fe; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1413 | status = ocfs2_journal_access_di(handle, old_dir, |
| 1414 | old_dir_bh, |
| 1415 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1416 | fe = (struct ocfs2_dinode *) old_dir_bh->b_data; |
| 1417 | fe->i_links_count = cpu_to_le16(old_dir->i_nlink); |
| 1418 | status = ocfs2_journal_dirty(handle, old_dir_bh); |
| 1419 | } |
| 1420 | } |
| 1421 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1422 | ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1423 | status = 0; |
| 1424 | bail: |
| 1425 | if (rename_lock) |
| 1426 | ocfs2_rename_unlock(osb); |
| 1427 | |
| 1428 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1429 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1430 | |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1431 | if (parents_locked) |
| 1432 | ocfs2_double_unlock(old_dir, new_dir); |
| 1433 | |
| 1434 | if (old_child_locked) |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1435 | ocfs2_inode_unlock(old_inode, 1); |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1436 | |
| 1437 | if (new_child_locked) |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1438 | ocfs2_inode_unlock(new_inode, 1); |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1439 | |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1440 | if (orphan_dir) { |
| 1441 | /* This was locked for us in ocfs2_prepare_orphan_dir() */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1442 | ocfs2_inode_unlock(orphan_dir, 1); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1443 | mutex_unlock(&orphan_dir->i_mutex); |
| 1444 | iput(orphan_dir); |
| 1445 | } |
| 1446 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1447 | if (new_inode) |
| 1448 | sync_mapping_buffers(old_inode->i_mapping); |
| 1449 | |
| 1450 | if (new_inode) |
| 1451 | iput(new_inode); |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1452 | |
| 1453 | ocfs2_free_dir_lookup_result(&target_lookup_res); |
| 1454 | ocfs2_free_dir_lookup_result(&old_entry_lookup); |
| 1455 | ocfs2_free_dir_lookup_result(&old_inode_dot_dot_res); |
| 1456 | ocfs2_free_dir_lookup_result(&orphan_insert); |
| 1457 | ocfs2_free_dir_lookup_result(&target_insert); |
| 1458 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1459 | brelse(newfe_bh); |
| 1460 | brelse(old_inode_bh); |
| 1461 | brelse(old_dir_bh); |
| 1462 | brelse(new_dir_bh); |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1463 | brelse(orphan_entry_bh); |
| 1464 | brelse(insert_entry_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1465 | |
| 1466 | mlog_exit(status); |
| 1467 | |
| 1468 | return status; |
| 1469 | } |
| 1470 | |
| 1471 | /* |
| 1472 | * we expect i_size = strlen(symname). Copy symname into the file |
| 1473 | * data, including the null terminator. |
| 1474 | */ |
| 1475 | static int ocfs2_create_symlink_data(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1476 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1477 | struct inode *inode, |
| 1478 | const char *symname) |
| 1479 | { |
| 1480 | struct buffer_head **bhs = NULL; |
| 1481 | const char *c; |
| 1482 | struct super_block *sb = osb->sb; |
Mark Fasheh | 4f902c3 | 2007-03-09 16:26:50 -0800 | [diff] [blame] | 1483 | u64 p_blkno, p_blocks; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1484 | int virtual, blocks, status, i, bytes_left; |
| 1485 | |
| 1486 | bytes_left = i_size_read(inode) + 1; |
| 1487 | /* we can't trust i_blocks because we're actually going to |
| 1488 | * write i_size + 1 bytes. */ |
| 1489 | blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 1490 | |
Andrew Morton | 5515eff | 2006-03-26 01:37:53 -0800 | [diff] [blame] | 1491 | mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n", |
| 1492 | (unsigned long long)inode->i_blocks, |
| 1493 | i_size_read(inode), blocks); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1494 | |
| 1495 | /* Sanity check -- make sure we're going to fit. */ |
| 1496 | if (bytes_left > |
| 1497 | ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) { |
| 1498 | status = -EIO; |
| 1499 | mlog_errno(status); |
| 1500 | goto bail; |
| 1501 | } |
| 1502 | |
| 1503 | bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL); |
| 1504 | if (!bhs) { |
| 1505 | status = -ENOMEM; |
| 1506 | mlog_errno(status); |
| 1507 | goto bail; |
| 1508 | } |
| 1509 | |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 1510 | status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks, |
| 1511 | NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1512 | if (status < 0) { |
| 1513 | mlog_errno(status); |
| 1514 | goto bail; |
| 1515 | } |
| 1516 | |
| 1517 | /* links can never be larger than one cluster so we know this |
| 1518 | * is all going to be contiguous, but do a sanity check |
| 1519 | * anyway. */ |
| 1520 | if ((p_blocks << sb->s_blocksize_bits) < bytes_left) { |
| 1521 | status = -EIO; |
| 1522 | mlog_errno(status); |
| 1523 | goto bail; |
| 1524 | } |
| 1525 | |
| 1526 | virtual = 0; |
| 1527 | while(bytes_left > 0) { |
| 1528 | c = &symname[virtual * sb->s_blocksize]; |
| 1529 | |
| 1530 | bhs[virtual] = sb_getblk(sb, p_blkno); |
| 1531 | if (!bhs[virtual]) { |
| 1532 | status = -ENOMEM; |
| 1533 | mlog_errno(status); |
| 1534 | goto bail; |
| 1535 | } |
| 1536 | ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]); |
| 1537 | |
| 1538 | status = ocfs2_journal_access(handle, inode, bhs[virtual], |
| 1539 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 1540 | if (status < 0) { |
| 1541 | mlog_errno(status); |
| 1542 | goto bail; |
| 1543 | } |
| 1544 | |
| 1545 | memset(bhs[virtual]->b_data, 0, sb->s_blocksize); |
| 1546 | |
| 1547 | memcpy(bhs[virtual]->b_data, c, |
| 1548 | (bytes_left > sb->s_blocksize) ? sb->s_blocksize : |
| 1549 | bytes_left); |
| 1550 | |
| 1551 | status = ocfs2_journal_dirty(handle, bhs[virtual]); |
| 1552 | if (status < 0) { |
| 1553 | mlog_errno(status); |
| 1554 | goto bail; |
| 1555 | } |
| 1556 | |
| 1557 | virtual++; |
| 1558 | p_blkno++; |
| 1559 | bytes_left -= sb->s_blocksize; |
| 1560 | } |
| 1561 | |
| 1562 | status = 0; |
| 1563 | bail: |
| 1564 | |
| 1565 | if (bhs) { |
| 1566 | for(i = 0; i < blocks; i++) |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1567 | brelse(bhs[i]); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1568 | kfree(bhs); |
| 1569 | } |
| 1570 | |
| 1571 | mlog_exit(status); |
| 1572 | return status; |
| 1573 | } |
| 1574 | |
| 1575 | static int ocfs2_symlink(struct inode *dir, |
| 1576 | struct dentry *dentry, |
| 1577 | const char *symname) |
| 1578 | { |
| 1579 | int status, l, credits; |
| 1580 | u64 newsize; |
| 1581 | struct ocfs2_super *osb = NULL; |
| 1582 | struct inode *inode = NULL; |
| 1583 | struct super_block *sb; |
| 1584 | struct buffer_head *new_fe_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1585 | struct buffer_head *parent_fe_bh = NULL; |
| 1586 | struct ocfs2_dinode *fe = NULL; |
| 1587 | struct ocfs2_dinode *dirfe; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1588 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1589 | struct ocfs2_alloc_context *inode_ac = NULL; |
| 1590 | struct ocfs2_alloc_context *data_ac = NULL; |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 1591 | struct ocfs2_alloc_context *xattr_ac = NULL; |
| 1592 | int want_clusters = 0; |
| 1593 | int xattr_credits = 0; |
| 1594 | struct ocfs2_security_xattr_info si = { |
| 1595 | .enable = 1, |
| 1596 | }; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1597 | int did_quota = 0, did_quota_inode = 0; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1598 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1599 | |
| 1600 | mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir, |
| 1601 | dentry, symname, dentry->d_name.len, dentry->d_name.name); |
| 1602 | |
| 1603 | sb = dir->i_sb; |
| 1604 | osb = OCFS2_SB(sb); |
| 1605 | |
| 1606 | l = strlen(symname) + 1; |
| 1607 | |
| 1608 | credits = ocfs2_calc_symlink_credits(sb); |
| 1609 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1610 | /* lock the parent directory */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1611 | status = ocfs2_inode_lock(dir, &parent_fe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1612 | if (status < 0) { |
| 1613 | if (status != -ENOENT) |
| 1614 | mlog_errno(status); |
Mark Fasheh | 6d8fc40 | 2006-10-06 11:54:33 -0700 | [diff] [blame] | 1615 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
| 1619 | if (!dirfe->i_links_count) { |
| 1620 | /* can't make a file in a deleted directory. */ |
| 1621 | status = -ENOENT; |
| 1622 | goto bail; |
| 1623 | } |
| 1624 | |
| 1625 | status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, |
| 1626 | dentry->d_name.len); |
| 1627 | if (status) |
| 1628 | goto bail; |
| 1629 | |
| 1630 | status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, |
| 1631 | dentry->d_name.name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1632 | dentry->d_name.len, &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1633 | if (status < 0) { |
| 1634 | mlog_errno(status); |
| 1635 | goto bail; |
| 1636 | } |
| 1637 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1638 | status = ocfs2_reserve_new_inode(osb, &inode_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1639 | if (status < 0) { |
| 1640 | if (status != -ENOSPC) |
| 1641 | mlog_errno(status); |
| 1642 | goto bail; |
| 1643 | } |
| 1644 | |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 1645 | inode = ocfs2_get_init_inode(dir, S_IFLNK | S_IRWXUGO); |
| 1646 | if (!inode) { |
| 1647 | status = -ENOMEM; |
| 1648 | mlog_errno(status); |
| 1649 | goto bail; |
| 1650 | } |
| 1651 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 1652 | /* get security xattr */ |
| 1653 | status = ocfs2_init_security_get(inode, dir, &si); |
| 1654 | if (status) { |
| 1655 | if (status == -EOPNOTSUPP) |
| 1656 | si.enable = 0; |
| 1657 | else { |
| 1658 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1659 | goto bail; |
| 1660 | } |
| 1661 | } |
| 1662 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 1663 | /* calculate meta data/clusters for setting security xattr */ |
| 1664 | if (si.enable) { |
| 1665 | status = ocfs2_calc_security_init(dir, &si, &want_clusters, |
| 1666 | &xattr_credits, &xattr_ac); |
| 1667 | if (status < 0) { |
| 1668 | mlog_errno(status); |
| 1669 | goto bail; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | /* don't reserve bitmap space for fast symlinks. */ |
| 1674 | if (l > ocfs2_fast_symlink_chars(sb)) |
| 1675 | want_clusters += 1; |
| 1676 | |
| 1677 | status = ocfs2_reserve_clusters(osb, want_clusters, &data_ac); |
| 1678 | if (status < 0) { |
| 1679 | if (status != -ENOSPC) |
| 1680 | mlog_errno(status); |
| 1681 | goto bail; |
| 1682 | } |
| 1683 | |
| 1684 | handle = ocfs2_start_trans(osb, credits + xattr_credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1685 | if (IS_ERR(handle)) { |
| 1686 | status = PTR_ERR(handle); |
| 1687 | handle = NULL; |
| 1688 | mlog_errno(status); |
| 1689 | goto bail; |
| 1690 | } |
| 1691 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1692 | /* We don't use standard VFS wrapper because we don't want vfs_dq_init |
| 1693 | * to be called. */ |
| 1694 | if (sb_any_quota_active(osb->sb) && |
| 1695 | osb->sb->dq_op->alloc_inode(inode, 1) == NO_QUOTA) { |
| 1696 | status = -EDQUOT; |
| 1697 | goto bail; |
| 1698 | } |
| 1699 | did_quota_inode = 1; |
| 1700 | |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 1701 | status = ocfs2_mknod_locked(osb, dir, inode, dentry, |
| 1702 | 0, &new_fe_bh, parent_fe_bh, handle, |
| 1703 | inode_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1704 | if (status < 0) { |
| 1705 | mlog_errno(status); |
| 1706 | goto bail; |
| 1707 | } |
| 1708 | |
| 1709 | fe = (struct ocfs2_dinode *) new_fe_bh->b_data; |
| 1710 | inode->i_rdev = 0; |
| 1711 | newsize = l - 1; |
| 1712 | if (l > ocfs2_fast_symlink_chars(sb)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1713 | u32 offset = 0; |
| 1714 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1715 | inode->i_op = &ocfs2_symlink_inode_operations; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1716 | if (vfs_dq_alloc_space_nodirty(inode, |
| 1717 | ocfs2_clusters_to_bytes(osb->sb, 1))) { |
| 1718 | status = -EDQUOT; |
| 1719 | goto bail; |
| 1720 | } |
| 1721 | did_quota = 1; |
Tao Ma | 0eb8d47e | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 1722 | status = ocfs2_add_inode_data(osb, inode, &offset, 1, 0, |
| 1723 | new_fe_bh, |
| 1724 | handle, data_ac, NULL, |
| 1725 | NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1726 | if (status < 0) { |
| 1727 | if (status != -ENOSPC && status != -EINTR) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1728 | mlog(ML_ERROR, |
| 1729 | "Failed to extend file to %llu\n", |
| 1730 | (unsigned long long)newsize); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1731 | mlog_errno(status); |
| 1732 | status = -ENOSPC; |
| 1733 | } |
| 1734 | goto bail; |
| 1735 | } |
| 1736 | i_size_write(inode, newsize); |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1737 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1738 | } else { |
| 1739 | inode->i_op = &ocfs2_fast_symlink_inode_operations; |
| 1740 | memcpy((char *) fe->id2.i_symlink, symname, l); |
| 1741 | i_size_write(inode, newsize); |
| 1742 | inode->i_blocks = 0; |
| 1743 | } |
| 1744 | |
| 1745 | status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh); |
| 1746 | if (status < 0) { |
| 1747 | mlog_errno(status); |
| 1748 | goto bail; |
| 1749 | } |
| 1750 | |
| 1751 | if (!ocfs2_inode_is_fast_symlink(inode)) { |
| 1752 | status = ocfs2_create_symlink_data(osb, handle, inode, |
| 1753 | symname); |
| 1754 | if (status < 0) { |
| 1755 | mlog_errno(status); |
| 1756 | goto bail; |
| 1757 | } |
| 1758 | } |
| 1759 | |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 1760 | if (si.enable) { |
| 1761 | status = ocfs2_init_security_set(handle, inode, new_fe_bh, &si, |
| 1762 | xattr_ac, data_ac); |
| 1763 | if (status < 0) { |
| 1764 | mlog_errno(status); |
| 1765 | goto bail; |
| 1766 | } |
| 1767 | } |
| 1768 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1769 | status = ocfs2_add_entry(handle, dentry, inode, |
| 1770 | le64_to_cpu(fe->i_blkno), parent_fe_bh, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1771 | &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1772 | if (status < 0) { |
| 1773 | mlog_errno(status); |
| 1774 | goto bail; |
| 1775 | } |
| 1776 | |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 1777 | status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1778 | if (status) { |
| 1779 | mlog_errno(status); |
| 1780 | goto bail; |
| 1781 | } |
| 1782 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1783 | insert_inode_hash(inode); |
| 1784 | dentry->d_op = &ocfs2_dentry_ops; |
| 1785 | d_instantiate(dentry, inode); |
| 1786 | bail: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1787 | if (status < 0 && did_quota) |
| 1788 | vfs_dq_free_space_nodirty(inode, |
| 1789 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 1790 | if (status < 0 && did_quota_inode) |
| 1791 | vfs_dq_free_inode(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1792 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1793 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | 6d8fc40 | 2006-10-06 11:54:33 -0700 | [diff] [blame] | 1794 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1795 | ocfs2_inode_unlock(dir, 1); |
Mark Fasheh | 6d8fc40 | 2006-10-06 11:54:33 -0700 | [diff] [blame] | 1796 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1797 | brelse(new_fe_bh); |
| 1798 | brelse(parent_fe_bh); |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 1799 | kfree(si.name); |
| 1800 | kfree(si.value); |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1801 | ocfs2_free_dir_lookup_result(&lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1802 | if (inode_ac) |
| 1803 | ocfs2_free_alloc_context(inode_ac); |
| 1804 | if (data_ac) |
| 1805 | ocfs2_free_alloc_context(data_ac); |
Tiger Yang | 534eadd | 2008-11-14 11:16:41 +0800 | [diff] [blame] | 1806 | if (xattr_ac) |
| 1807 | ocfs2_free_alloc_context(xattr_ac); |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 1808 | if ((status < 0) && inode) { |
| 1809 | clear_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1810 | iput(inode); |
Tiger Yang | f5d3620 | 2008-11-14 11:15:44 +0800 | [diff] [blame] | 1811 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1812 | |
| 1813 | mlog_exit(status); |
| 1814 | |
| 1815 | return status; |
| 1816 | } |
| 1817 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1818 | static int ocfs2_blkno_stringify(u64 blkno, char *name) |
| 1819 | { |
| 1820 | int status, namelen; |
| 1821 | |
| 1822 | mlog_entry_void(); |
| 1823 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1824 | namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx", |
| 1825 | (long long)blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1826 | if (namelen <= 0) { |
| 1827 | if (namelen) |
| 1828 | status = namelen; |
| 1829 | else |
| 1830 | status = -EINVAL; |
| 1831 | mlog_errno(status); |
| 1832 | goto bail; |
| 1833 | } |
| 1834 | if (namelen != OCFS2_ORPHAN_NAMELEN) { |
| 1835 | status = -EINVAL; |
| 1836 | mlog_errno(status); |
| 1837 | goto bail; |
| 1838 | } |
| 1839 | |
| 1840 | mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name, |
| 1841 | namelen); |
| 1842 | |
| 1843 | status = 0; |
| 1844 | bail: |
| 1845 | mlog_exit(status); |
| 1846 | return status; |
| 1847 | } |
| 1848 | |
| 1849 | static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1850 | struct inode **ret_orphan_dir, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1851 | struct inode *inode, |
| 1852 | char *name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1853 | struct ocfs2_dir_lookup_result *lookup) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1854 | { |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1855 | struct inode *orphan_dir_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1856 | struct buffer_head *orphan_dir_bh = NULL; |
| 1857 | int status = 0; |
| 1858 | |
| 1859 | status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name); |
| 1860 | if (status < 0) { |
| 1861 | mlog_errno(status); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1862 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | orphan_dir_inode = ocfs2_get_system_file_inode(osb, |
| 1866 | ORPHAN_DIR_SYSTEM_INODE, |
| 1867 | osb->slot_num); |
| 1868 | if (!orphan_dir_inode) { |
| 1869 | status = -ENOENT; |
| 1870 | mlog_errno(status); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1871 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1874 | mutex_lock(&orphan_dir_inode->i_mutex); |
| 1875 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1876 | status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1877 | if (status < 0) { |
| 1878 | mlog_errno(status); |
| 1879 | goto leave; |
| 1880 | } |
| 1881 | |
| 1882 | status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode, |
| 1883 | orphan_dir_bh, name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1884 | OCFS2_ORPHAN_NAMELEN, lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1885 | if (status < 0) { |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1886 | ocfs2_inode_unlock(orphan_dir_inode, 1); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1887 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1888 | mlog_errno(status); |
| 1889 | goto leave; |
| 1890 | } |
| 1891 | |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1892 | *ret_orphan_dir = orphan_dir_inode; |
| 1893 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1894 | leave: |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1895 | if (status) { |
| 1896 | mutex_unlock(&orphan_dir_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1897 | iput(orphan_dir_inode); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1898 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1899 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1900 | brelse(orphan_dir_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1901 | |
| 1902 | mlog_exit(status); |
| 1903 | return status; |
| 1904 | } |
| 1905 | |
| 1906 | static int ocfs2_orphan_add(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1907 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1908 | struct inode *inode, |
| 1909 | struct ocfs2_dinode *fe, |
| 1910 | char *name, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1911 | struct ocfs2_dir_lookup_result *lookup, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1912 | struct inode *orphan_dir_inode) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1913 | { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1914 | struct buffer_head *orphan_dir_bh = NULL; |
| 1915 | int status = 0; |
| 1916 | struct ocfs2_dinode *orphan_fe; |
| 1917 | |
| 1918 | mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino); |
| 1919 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1920 | status = ocfs2_read_inode_block(orphan_dir_inode, &orphan_dir_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1921 | if (status < 0) { |
| 1922 | mlog_errno(status); |
| 1923 | goto leave; |
| 1924 | } |
| 1925 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1926 | status = ocfs2_journal_access_di(handle, orphan_dir_inode, orphan_dir_bh, |
| 1927 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1928 | if (status < 0) { |
| 1929 | mlog_errno(status); |
| 1930 | goto leave; |
| 1931 | } |
| 1932 | |
| 1933 | /* we're a cluster, and nlink can change on disk from |
| 1934 | * underneath us... */ |
| 1935 | orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data; |
| 1936 | if (S_ISDIR(inode->i_mode)) |
| 1937 | le16_add_cpu(&orphan_fe->i_links_count, 1); |
| 1938 | orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count); |
| 1939 | |
| 1940 | status = ocfs2_journal_dirty(handle, orphan_dir_bh); |
| 1941 | if (status < 0) { |
| 1942 | mlog_errno(status); |
| 1943 | goto leave; |
| 1944 | } |
| 1945 | |
| 1946 | status = __ocfs2_add_entry(handle, orphan_dir_inode, name, |
| 1947 | OCFS2_ORPHAN_NAMELEN, inode, |
| 1948 | OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1949 | orphan_dir_bh, lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1950 | if (status < 0) { |
| 1951 | mlog_errno(status); |
| 1952 | goto leave; |
| 1953 | } |
| 1954 | |
| 1955 | le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL); |
| 1956 | |
| 1957 | /* Record which orphan dir our inode now resides |
| 1958 | * in. delete_inode will use this to determine which orphan |
| 1959 | * dir to lock. */ |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 1960 | fe->i_orphaned_slot = cpu_to_le16(osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1961 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1962 | mlog(0, "Inode %llu orphaned in slot %d\n", |
| 1963 | (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1964 | |
| 1965 | leave: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1966 | brelse(orphan_dir_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1967 | |
| 1968 | mlog_exit(status); |
| 1969 | return status; |
| 1970 | } |
| 1971 | |
| 1972 | /* unlike orphan_add, we expect the orphan dir to already be locked here. */ |
| 1973 | int ocfs2_orphan_del(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1974 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1975 | struct inode *orphan_dir_inode, |
| 1976 | struct inode *inode, |
| 1977 | struct buffer_head *orphan_dir_bh) |
| 1978 | { |
| 1979 | char name[OCFS2_ORPHAN_NAMELEN + 1]; |
| 1980 | struct ocfs2_dinode *orphan_fe; |
| 1981 | int status = 0; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1982 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1983 | |
| 1984 | mlog_entry_void(); |
| 1985 | |
| 1986 | status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name); |
| 1987 | if (status < 0) { |
| 1988 | mlog_errno(status); |
| 1989 | goto leave; |
| 1990 | } |
| 1991 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1992 | mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n", |
| 1993 | name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno, |
| 1994 | OCFS2_ORPHAN_NAMELEN); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1995 | |
| 1996 | /* find it's spot in the orphan directory */ |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1997 | status = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, orphan_dir_inode, |
| 1998 | &lookup); |
| 1999 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2000 | mlog_errno(status); |
| 2001 | goto leave; |
| 2002 | } |
| 2003 | |
| 2004 | /* remove it from the orphan directory */ |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 2005 | status = ocfs2_delete_entry(handle, orphan_dir_inode, &lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2006 | if (status < 0) { |
| 2007 | mlog_errno(status); |
| 2008 | goto leave; |
| 2009 | } |
| 2010 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2011 | status = ocfs2_journal_access_di(handle,orphan_dir_inode, orphan_dir_bh, |
| 2012 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2013 | if (status < 0) { |
| 2014 | mlog_errno(status); |
| 2015 | goto leave; |
| 2016 | } |
| 2017 | |
| 2018 | /* do the i_nlink dance! :) */ |
| 2019 | orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data; |
| 2020 | if (S_ISDIR(inode->i_mode)) |
| 2021 | le16_add_cpu(&orphan_fe->i_links_count, -1); |
| 2022 | orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count); |
| 2023 | |
| 2024 | status = ocfs2_journal_dirty(handle, orphan_dir_bh); |
| 2025 | if (status < 0) { |
| 2026 | mlog_errno(status); |
| 2027 | goto leave; |
| 2028 | } |
| 2029 | |
| 2030 | leave: |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 2031 | ocfs2_free_dir_lookup_result(&lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2032 | |
| 2033 | mlog_exit(status); |
| 2034 | return status; |
| 2035 | } |
| 2036 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2037 | const struct inode_operations ocfs2_dir_iops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2038 | .create = ocfs2_create, |
| 2039 | .lookup = ocfs2_lookup, |
| 2040 | .link = ocfs2_link, |
| 2041 | .unlink = ocfs2_unlink, |
| 2042 | .rmdir = ocfs2_unlink, |
| 2043 | .symlink = ocfs2_symlink, |
| 2044 | .mkdir = ocfs2_mkdir, |
| 2045 | .mknod = ocfs2_mknod, |
| 2046 | .rename = ocfs2_rename, |
| 2047 | .setattr = ocfs2_setattr, |
| 2048 | .getattr = ocfs2_getattr, |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 2049 | .permission = ocfs2_permission, |
Tiger Yang | cf1d6c7 | 2008-08-18 17:11:00 +0800 | [diff] [blame] | 2050 | .setxattr = generic_setxattr, |
| 2051 | .getxattr = generic_getxattr, |
| 2052 | .listxattr = ocfs2_listxattr, |
| 2053 | .removexattr = generic_removexattr, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2054 | }; |