David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/completion.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/namei.h> |
| 15 | #include <linux/utsname.h> |
| 16 | #include <linux/mm.h> |
| 17 | #include <linux/xattr.h> |
| 18 | #include <linux/posix_acl.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 20 | #include <linux/crc32.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 21 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #include <asm/uaccess.h> |
| 23 | |
| 24 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 25 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 26 | #include "acl.h" |
| 27 | #include "bmap.h" |
| 28 | #include "dir.h" |
| 29 | #include "eaops.h" |
| 30 | #include "eattr.h" |
| 31 | #include "glock.h" |
| 32 | #include "inode.h" |
| 33 | #include "meta_io.h" |
| 34 | #include "ops_dentry.h" |
| 35 | #include "ops_inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | #include "quota.h" |
| 37 | #include "rgrp.h" |
| 38 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 39 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * gfs2_create - Create a file |
| 43 | * @dir: The directory in which to create the file |
| 44 | * @dentry: The dentry of the new file |
| 45 | * @mode: The mode of the new file |
| 46 | * |
| 47 | * Returns: errno |
| 48 | */ |
| 49 | |
| 50 | static int gfs2_create(struct inode *dir, struct dentry *dentry, |
| 51 | int mode, struct nameidata *nd) |
| 52 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 53 | struct gfs2_inode *dip = GFS2_I(dir); |
| 54 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 55 | struct gfs2_holder ghs[2]; |
| 56 | struct inode *inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 57 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 58 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 59 | |
| 60 | for (;;) { |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 61 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 62 | if (!IS_ERR(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 63 | gfs2_trans_end(sdp); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 64 | if (dip->i_alloc->al_rgd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 65 | gfs2_inplace_release(dip); |
| 66 | gfs2_quota_unlock(dip); |
| 67 | gfs2_alloc_put(dip); |
| 68 | gfs2_glock_dq_uninit_m(2, ghs); |
Steven Whitehouse | 3a8476d | 2006-06-19 09:10:39 -0400 | [diff] [blame] | 69 | mark_inode_dirty(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 70 | break; |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 71 | } else if (PTR_ERR(inode) != -EEXIST || |
Al Viro | 3516586 | 2008-08-05 03:00:49 -0400 | [diff] [blame] | 72 | (nd && nd->flags & LOOKUP_EXCL)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 73 | gfs2_holder_uninit(ghs); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 74 | return PTR_ERR(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Al Viro | a569c71 | 2008-07-23 14:42:05 -0400 | [diff] [blame] | 77 | inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 78 | if (inode) { |
| 79 | if (!IS_ERR(inode)) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 80 | gfs2_holder_uninit(ghs); |
| 81 | break; |
| 82 | } else { |
| 83 | gfs2_holder_uninit(ghs); |
| 84 | return PTR_ERR(inode); |
| 85 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 89 | d_instantiate(dentry, inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * gfs2_lookup - Look up a filename in a directory and return its inode |
| 96 | * @dir: The directory inode |
| 97 | * @dentry: The dentry of the new inode |
| 98 | * @nd: passed from Linux VFS, ignored by us |
| 99 | * |
| 100 | * Called by the VFS layer. Lock dir and call gfs2_lookupi() |
| 101 | * |
| 102 | * Returns: errno |
| 103 | */ |
| 104 | |
| 105 | static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry, |
| 106 | struct nameidata *nd) |
| 107 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 108 | struct inode *inode = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 109 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 110 | dentry->d_op = &gfs2_dops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 111 | |
Al Viro | a569c71 | 2008-07-23 14:42:05 -0400 | [diff] [blame] | 112 | inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 113 | if (inode && IS_ERR(inode)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 114 | return ERR_CAST(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 115 | |
Steven Whitehouse | 9656b2c | 2008-01-08 08:14:30 +0000 | [diff] [blame] | 116 | if (inode) { |
| 117 | struct gfs2_glock *gl = GFS2_I(inode)->i_gl; |
| 118 | struct gfs2_holder gh; |
| 119 | int error; |
| 120 | error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
| 121 | if (error) { |
| 122 | iput(inode); |
| 123 | return ERR_PTR(error); |
| 124 | } |
| 125 | gfs2_glock_dq_uninit(&gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 126 | return d_splice_alias(inode, dentry); |
Steven Whitehouse | 9656b2c | 2008-01-08 08:14:30 +0000 | [diff] [blame] | 127 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 128 | d_add(dentry, inode); |
| 129 | |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * gfs2_link - Link to a file |
| 135 | * @old_dentry: The inode to link |
| 136 | * @dir: Add link to this directory |
| 137 | * @dentry: The name of the link |
| 138 | * |
| 139 | * Link the inode in "old_dentry" into the directory "dir" with the |
| 140 | * name in "dentry". |
| 141 | * |
| 142 | * Returns: errno |
| 143 | */ |
| 144 | |
| 145 | static int gfs2_link(struct dentry *old_dentry, struct inode *dir, |
| 146 | struct dentry *dentry) |
| 147 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 148 | struct gfs2_inode *dip = GFS2_I(dir); |
| 149 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 150 | struct inode *inode = old_dentry->d_inode; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 151 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 152 | struct gfs2_holder ghs[2]; |
| 153 | int alloc_required; |
| 154 | int error; |
| 155 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 156 | if (S_ISDIR(inode->i_mode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 157 | return -EPERM; |
| 158 | |
| 159 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 160 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 161 | |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 162 | error = gfs2_glock_nq(ghs); /* parent */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 163 | if (error) |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 164 | goto out_parent; |
| 165 | |
| 166 | error = gfs2_glock_nq(ghs + 1); /* child */ |
| 167 | if (error) |
| 168 | goto out_child; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 169 | |
Miklos Szeredi | f58ba88 | 2008-07-02 21:12:01 +0200 | [diff] [blame] | 170 | error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 171 | if (error) |
| 172 | goto out_gunlock; |
| 173 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 174 | error = gfs2_dir_check(dir, &dentry->d_name, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 175 | switch (error) { |
| 176 | case -ENOENT: |
| 177 | break; |
| 178 | case 0: |
| 179 | error = -EEXIST; |
| 180 | default: |
| 181 | goto out_gunlock; |
| 182 | } |
| 183 | |
| 184 | error = -EINVAL; |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 185 | if (!dip->i_inode.i_nlink) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 186 | goto out_gunlock; |
| 187 | error = -EFBIG; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 188 | if (dip->i_di.di_entries == (u32)-1) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 189 | goto out_gunlock; |
| 190 | error = -EPERM; |
| 191 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 192 | goto out_gunlock; |
| 193 | error = -EINVAL; |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 194 | if (!ip->i_inode.i_nlink) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 195 | goto out_gunlock; |
| 196 | error = -EMLINK; |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 197 | if (ip->i_inode.i_nlink == (u32)-1) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 198 | goto out_gunlock; |
| 199 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 200 | alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name); |
| 201 | if (error < 0) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 202 | goto out_gunlock; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 203 | error = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 204 | |
| 205 | if (alloc_required) { |
| 206 | struct gfs2_alloc *al = gfs2_alloc_get(dip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 207 | if (!al) { |
| 208 | error = -ENOMEM; |
| 209 | goto out_gunlock; |
| 210 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 211 | |
Steven Whitehouse | d82661d | 2008-03-10 15:34:50 +0000 | [diff] [blame] | 212 | error = gfs2_quota_lock_check(dip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 213 | if (error) |
| 214 | goto out_alloc; |
| 215 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 216 | al->al_requested = sdp->sd_max_dirres; |
| 217 | |
| 218 | error = gfs2_inplace_reserve(dip); |
| 219 | if (error) |
| 220 | goto out_gunlock_q; |
| 221 | |
Steven Whitehouse | 1b50259 | 2006-05-18 14:10:52 -0400 | [diff] [blame] | 222 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 223 | al->al_rgd->rd_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 224 | 2 * RES_DINODE + RES_STATFS + |
| 225 | RES_QUOTA, 0); |
| 226 | if (error) |
| 227 | goto out_ipres; |
| 228 | } else { |
| 229 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0); |
| 230 | if (error) |
| 231 | goto out_ipres; |
| 232 | } |
| 233 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 234 | error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 235 | if (error) |
| 236 | goto out_end_trans; |
| 237 | |
| 238 | error = gfs2_change_nlink(ip, +1); |
| 239 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 240 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 241 | gfs2_trans_end(sdp); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 242 | out_ipres: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 243 | if (alloc_required) |
| 244 | gfs2_inplace_release(dip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 245 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 246 | if (alloc_required) |
| 247 | gfs2_quota_unlock(dip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 248 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 249 | if (alloc_required) |
| 250 | gfs2_alloc_put(dip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 251 | out_gunlock: |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 252 | gfs2_glock_dq(ghs + 1); |
| 253 | out_child: |
| 254 | gfs2_glock_dq(ghs); |
| 255 | out_parent: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 256 | gfs2_holder_uninit(ghs); |
| 257 | gfs2_holder_uninit(ghs + 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 258 | if (!error) { |
Steven Whitehouse | 29937ac | 2006-07-06 17:58:03 -0400 | [diff] [blame] | 259 | atomic_inc(&inode->i_count); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 260 | d_instantiate(dentry, inode); |
| 261 | mark_inode_dirty(inode); |
| 262 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 263 | return error; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * gfs2_unlink - Unlink a file |
| 268 | * @dir: The inode of the directory containing the file to unlink |
| 269 | * @dentry: The file itself |
| 270 | * |
| 271 | * Unlink a file. Call gfs2_unlinki() |
| 272 | * |
| 273 | * Returns: errno |
| 274 | */ |
| 275 | |
| 276 | static int gfs2_unlink(struct inode *dir, struct dentry *dentry) |
| 277 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 278 | struct gfs2_inode *dip = GFS2_I(dir); |
| 279 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 280 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 281 | struct gfs2_holder ghs[3]; |
| 282 | struct gfs2_rgrpd *rgd; |
| 283 | struct gfs2_holder ri_gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 284 | int error; |
| 285 | |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 286 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 287 | if (error) |
| 288 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 289 | |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 290 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 291 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 292 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 293 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 294 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
| 295 | |
| 296 | |
Steven Whitehouse | 8497a46 | 2007-08-26 14:23:56 +0100 | [diff] [blame] | 297 | error = gfs2_glock_nq(ghs); /* parent */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 298 | if (error) |
Steven Whitehouse | 8497a46 | 2007-08-26 14:23:56 +0100 | [diff] [blame] | 299 | goto out_parent; |
| 300 | |
| 301 | error = gfs2_glock_nq(ghs + 1); /* child */ |
| 302 | if (error) |
| 303 | goto out_child; |
| 304 | |
| 305 | error = gfs2_glock_nq(ghs + 2); /* rgrp */ |
| 306 | if (error) |
| 307 | goto out_rgrp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 308 | |
| 309 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); |
| 310 | if (error) |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 311 | goto out_gunlock; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 312 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 313 | error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 314 | if (error) |
Steven Whitehouse | 8497a46 | 2007-08-26 14:23:56 +0100 | [diff] [blame] | 315 | goto out_rgrp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 316 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 317 | error = gfs2_dir_del(dip, &dentry->d_name); |
| 318 | if (error) |
| 319 | goto out_end_trans; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 320 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 321 | error = gfs2_change_nlink(ip, -1); |
| 322 | |
| 323 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 324 | gfs2_trans_end(sdp); |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 325 | out_gunlock: |
Steven Whitehouse | 8497a46 | 2007-08-26 14:23:56 +0100 | [diff] [blame] | 326 | gfs2_glock_dq(ghs + 2); |
| 327 | out_rgrp: |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 328 | gfs2_holder_uninit(ghs + 2); |
Steven Whitehouse | 8497a46 | 2007-08-26 14:23:56 +0100 | [diff] [blame] | 329 | gfs2_glock_dq(ghs + 1); |
| 330 | out_child: |
| 331 | gfs2_holder_uninit(ghs + 1); |
| 332 | gfs2_glock_dq(ghs); |
| 333 | out_parent: |
| 334 | gfs2_holder_uninit(ghs); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 335 | gfs2_glock_dq_uninit(&ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 336 | return error; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * gfs2_symlink - Create a symlink |
| 341 | * @dir: The directory to create the symlink in |
| 342 | * @dentry: The dentry to put the symlink in |
| 343 | * @symname: The thing which the link points to |
| 344 | * |
| 345 | * Returns: errno |
| 346 | */ |
| 347 | |
| 348 | static int gfs2_symlink(struct inode *dir, struct dentry *dentry, |
| 349 | const char *symname) |
| 350 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 351 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
| 352 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 353 | struct gfs2_holder ghs[2]; |
| 354 | struct inode *inode; |
| 355 | struct buffer_head *dibh; |
| 356 | int size; |
| 357 | int error; |
| 358 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 359 | /* Must be stuffed with a null terminator for gfs2_follow_link() */ |
| 360 | size = strlen(symname); |
| 361 | if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1) |
| 362 | return -ENAMETOOLONG; |
| 363 | |
| 364 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 365 | |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 366 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 367 | if (IS_ERR(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 368 | gfs2_holder_uninit(ghs); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 369 | return PTR_ERR(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 372 | ip = ghs[1].gh_gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 373 | |
| 374 | ip->i_di.di_size = size; |
| 375 | |
| 376 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 377 | |
| 378 | if (!gfs2_assert_withdraw(sdp, !error)) { |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 379 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 380 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, |
| 381 | size); |
| 382 | brelse(dibh); |
| 383 | } |
| 384 | |
| 385 | gfs2_trans_end(sdp); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 386 | if (dip->i_alloc->al_rgd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 387 | gfs2_inplace_release(dip); |
| 388 | gfs2_quota_unlock(dip); |
| 389 | gfs2_alloc_put(dip); |
| 390 | |
| 391 | gfs2_glock_dq_uninit_m(2, ghs); |
| 392 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 393 | d_instantiate(dentry, inode); |
| 394 | mark_inode_dirty(inode); |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * gfs2_mkdir - Make a directory |
| 401 | * @dir: The parent directory of the new one |
| 402 | * @dentry: The dentry of the new directory |
| 403 | * @mode: The mode of the new directory |
| 404 | * |
| 405 | * Returns: errno |
| 406 | */ |
| 407 | |
| 408 | static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 409 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 410 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
| 411 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 412 | struct gfs2_holder ghs[2]; |
| 413 | struct inode *inode; |
| 414 | struct buffer_head *dibh; |
| 415 | int error; |
| 416 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 417 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 418 | |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 419 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 420 | if (IS_ERR(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 421 | gfs2_holder_uninit(ghs); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 422 | return PTR_ERR(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 425 | ip = ghs[1].gh_gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 426 | |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 427 | ip->i_inode.i_nlink = 2; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 428 | ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode); |
| 429 | ip->i_di.di_flags |= GFS2_DIF_JDATA; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 430 | ip->i_di.di_entries = 2; |
| 431 | |
| 432 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 433 | |
| 434 | if (!gfs2_assert_withdraw(sdp, !error)) { |
| 435 | struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 436 | struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 437 | struct qstr str; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 438 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 439 | gfs2_str2qstr(&str, "."); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 440 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 441 | gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 442 | dent->de_inum = di->di_num; /* already GFS2 endian */ |
Steven Whitehouse | 7ecdb70 | 2006-10-03 21:03:35 -0400 | [diff] [blame] | 443 | dent->de_type = cpu_to_be16(DT_DIR); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 444 | di->di_entries = cpu_to_be32(1); |
| 445 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 446 | gfs2_str2qstr(&str, ".."); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 447 | dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1)); |
| 448 | gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 449 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 450 | gfs2_inum_out(dip, dent); |
Steven Whitehouse | 7ecdb70 | 2006-10-03 21:03:35 -0400 | [diff] [blame] | 451 | dent->de_type = cpu_to_be16(DT_DIR); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 452 | |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 453 | gfs2_dinode_out(ip, di); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 454 | |
| 455 | brelse(dibh); |
| 456 | } |
| 457 | |
| 458 | error = gfs2_change_nlink(dip, +1); |
| 459 | gfs2_assert_withdraw(sdp, !error); /* dip already pinned */ |
| 460 | |
| 461 | gfs2_trans_end(sdp); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 462 | if (dip->i_alloc->al_rgd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 463 | gfs2_inplace_release(dip); |
| 464 | gfs2_quota_unlock(dip); |
| 465 | gfs2_alloc_put(dip); |
| 466 | |
| 467 | gfs2_glock_dq_uninit_m(2, ghs); |
| 468 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 469 | d_instantiate(dentry, inode); |
| 470 | mark_inode_dirty(inode); |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * gfs2_rmdir - Remove a directory |
| 477 | * @dir: The parent directory of the directory to be removed |
| 478 | * @dentry: The dentry of the directory to remove |
| 479 | * |
| 480 | * Remove a directory. Call gfs2_rmdiri() |
| 481 | * |
| 482 | * Returns: errno |
| 483 | */ |
| 484 | |
| 485 | static int gfs2_rmdir(struct inode *dir, struct dentry *dentry) |
| 486 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 487 | struct gfs2_inode *dip = GFS2_I(dir); |
| 488 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 489 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 490 | struct gfs2_holder ghs[3]; |
| 491 | struct gfs2_rgrpd *rgd; |
| 492 | struct gfs2_holder ri_gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 493 | int error; |
| 494 | |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 495 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 496 | if (error) |
| 497 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 498 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 499 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 500 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 501 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 502 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
| 503 | |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 504 | error = gfs2_glock_nq(ghs); /* parent */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 505 | if (error) |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 506 | goto out_parent; |
| 507 | |
| 508 | error = gfs2_glock_nq(ghs + 1); /* child */ |
| 509 | if (error) |
| 510 | goto out_child; |
| 511 | |
| 512 | error = gfs2_glock_nq(ghs + 2); /* rgrp */ |
| 513 | if (error) |
| 514 | goto out_rgrp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 515 | |
| 516 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); |
| 517 | if (error) |
| 518 | goto out_gunlock; |
| 519 | |
| 520 | if (ip->i_di.di_entries < 2) { |
| 521 | if (gfs2_consist_inode(ip)) |
Steven Whitehouse | 4cc14f0 | 2006-10-31 19:00:24 -0500 | [diff] [blame] | 522 | gfs2_dinode_print(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 523 | error = -EIO; |
| 524 | goto out_gunlock; |
| 525 | } |
| 526 | if (ip->i_di.di_entries > 2) { |
| 527 | error = -ENOTEMPTY; |
| 528 | goto out_gunlock; |
| 529 | } |
| 530 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 531 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 532 | if (error) |
| 533 | goto out_gunlock; |
| 534 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 535 | error = gfs2_rmdiri(dip, &dentry->d_name, ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 536 | |
| 537 | gfs2_trans_end(sdp); |
| 538 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 539 | out_gunlock: |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 540 | gfs2_glock_dq(ghs + 2); |
| 541 | out_rgrp: |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 542 | gfs2_holder_uninit(ghs + 2); |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 543 | gfs2_glock_dq(ghs + 1); |
| 544 | out_child: |
| 545 | gfs2_holder_uninit(ghs + 1); |
| 546 | gfs2_glock_dq(ghs); |
| 547 | out_parent: |
| 548 | gfs2_holder_uninit(ghs); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 549 | gfs2_glock_dq_uninit(&ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 550 | return error; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * gfs2_mknod - Make a special file |
| 555 | * @dir: The directory in which the special file will reside |
| 556 | * @dentry: The dentry of the special file |
| 557 | * @mode: The mode of the special file |
| 558 | * @rdev: The device specification of the special file |
| 559 | * |
| 560 | */ |
| 561 | |
| 562 | static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 563 | dev_t dev) |
| 564 | { |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 565 | struct gfs2_inode *dip = GFS2_I(dir); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 566 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 567 | struct gfs2_holder ghs[2]; |
| 568 | struct inode *inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 569 | |
| 570 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 571 | |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 572 | inode = gfs2_createi(ghs, &dentry->d_name, mode, dev); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 573 | if (IS_ERR(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 574 | gfs2_holder_uninit(ghs); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 575 | return PTR_ERR(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 576 | } |
| 577 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 578 | gfs2_trans_end(sdp); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 579 | if (dip->i_alloc->al_rgd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 580 | gfs2_inplace_release(dip); |
| 581 | gfs2_quota_unlock(dip); |
| 582 | gfs2_alloc_put(dip); |
| 583 | |
| 584 | gfs2_glock_dq_uninit_m(2, ghs); |
| 585 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 586 | d_instantiate(dentry, inode); |
| 587 | mark_inode_dirty(inode); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
Steven Whitehouse | 0188d6c | 2008-08-26 09:38:26 +0100 | [diff] [blame] | 592 | /* |
| 593 | * gfs2_ok_to_move - check if it's ok to move a directory to another directory |
| 594 | * @this: move this |
| 595 | * @to: to here |
| 596 | * |
| 597 | * Follow @to back to the root and make sure we don't encounter @this |
| 598 | * Assumes we already hold the rename lock. |
| 599 | * |
| 600 | * Returns: errno |
| 601 | */ |
| 602 | |
| 603 | static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) |
| 604 | { |
| 605 | struct inode *dir = &to->i_inode; |
| 606 | struct super_block *sb = dir->i_sb; |
| 607 | struct inode *tmp; |
| 608 | struct qstr dotdot; |
| 609 | int error = 0; |
| 610 | |
| 611 | gfs2_str2qstr(&dotdot, ".."); |
| 612 | |
| 613 | igrab(dir); |
| 614 | |
| 615 | for (;;) { |
| 616 | if (dir == &this->i_inode) { |
| 617 | error = -EINVAL; |
| 618 | break; |
| 619 | } |
| 620 | if (dir == sb->s_root->d_inode) { |
| 621 | error = 0; |
| 622 | break; |
| 623 | } |
| 624 | |
| 625 | tmp = gfs2_lookupi(dir, &dotdot, 1); |
| 626 | if (IS_ERR(tmp)) { |
| 627 | error = PTR_ERR(tmp); |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | iput(dir); |
| 632 | dir = tmp; |
| 633 | } |
| 634 | |
| 635 | iput(dir); |
| 636 | |
| 637 | return error; |
| 638 | } |
| 639 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 640 | /** |
| 641 | * gfs2_rename - Rename a file |
| 642 | * @odir: Parent directory of old file name |
| 643 | * @odentry: The old dentry of the file |
| 644 | * @ndir: Parent directory of new file name |
| 645 | * @ndentry: The new dentry of the file |
| 646 | * |
| 647 | * Returns: errno |
| 648 | */ |
| 649 | |
| 650 | static int gfs2_rename(struct inode *odir, struct dentry *odentry, |
| 651 | struct inode *ndir, struct dentry *ndentry) |
| 652 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 653 | struct gfs2_inode *odip = GFS2_I(odir); |
| 654 | struct gfs2_inode *ndip = GFS2_I(ndir); |
| 655 | struct gfs2_inode *ip = GFS2_I(odentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 656 | struct gfs2_inode *nip = NULL; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 657 | struct gfs2_sbd *sdp = GFS2_SB(odir); |
Steven Whitehouse | 0188d6c | 2008-08-26 09:38:26 +0100 | [diff] [blame] | 658 | struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }; |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 659 | struct gfs2_rgrpd *nrgd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 660 | unsigned int num_gh; |
| 661 | int dir_rename = 0; |
| 662 | int alloc_required; |
| 663 | unsigned int x; |
| 664 | int error; |
| 665 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 666 | if (ndentry->d_inode) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 667 | nip = GFS2_I(ndentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 668 | if (ip == nip) |
| 669 | return 0; |
| 670 | } |
| 671 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 672 | |
Steven Whitehouse | 0188d6c | 2008-08-26 09:38:26 +0100 | [diff] [blame] | 673 | if (odip != ndip) { |
| 674 | error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, |
| 675 | 0, &r_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 676 | if (error) |
| 677 | goto out; |
| 678 | |
Steven Whitehouse | 0188d6c | 2008-08-26 09:38:26 +0100 | [diff] [blame] | 679 | if (S_ISDIR(ip->i_inode.i_mode)) { |
| 680 | dir_rename = 1; |
| 681 | /* don't move a dirctory into it's subdir */ |
| 682 | error = gfs2_ok_to_move(ip, ndip); |
| 683 | if (error) |
| 684 | goto out_gunlock_r; |
| 685 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Steven Whitehouse | d9d1ca3 | 2006-06-21 15:38:17 -0400 | [diff] [blame] | 688 | num_gh = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 689 | gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
Steven Whitehouse | d9d1ca3 | 2006-06-21 15:38:17 -0400 | [diff] [blame] | 690 | if (odip != ndip) { |
| 691 | gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 692 | num_gh++; |
| 693 | } |
| 694 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 695 | num_gh++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 696 | |
Steven Whitehouse | d9d1ca3 | 2006-06-21 15:38:17 -0400 | [diff] [blame] | 697 | if (nip) { |
| 698 | gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 699 | num_gh++; |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 700 | /* grab the resource lock for unlink flag twiddling |
| 701 | * this is the case of the target file already existing |
| 702 | * so we unlink before doing the rename |
| 703 | */ |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 704 | nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 705 | if (nrgd) |
| 706 | gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++); |
Steven Whitehouse | d9d1ca3 | 2006-06-21 15:38:17 -0400 | [diff] [blame] | 707 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 708 | |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 709 | for (x = 0; x < num_gh; x++) { |
| 710 | error = gfs2_glock_nq(ghs + x); |
| 711 | if (error) |
| 712 | goto out_gunlock; |
| 713 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 714 | |
| 715 | /* Check out the old directory */ |
| 716 | |
| 717 | error = gfs2_unlink_ok(odip, &odentry->d_name, ip); |
| 718 | if (error) |
| 719 | goto out_gunlock; |
| 720 | |
| 721 | /* Check out the new directory */ |
| 722 | |
| 723 | if (nip) { |
| 724 | error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); |
| 725 | if (error) |
| 726 | goto out_gunlock; |
| 727 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 728 | if (S_ISDIR(nip->i_inode.i_mode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 729 | if (nip->i_di.di_entries < 2) { |
| 730 | if (gfs2_consist_inode(nip)) |
Steven Whitehouse | 4cc14f0 | 2006-10-31 19:00:24 -0500 | [diff] [blame] | 731 | gfs2_dinode_print(nip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 732 | error = -EIO; |
| 733 | goto out_gunlock; |
| 734 | } |
| 735 | if (nip->i_di.di_entries > 2) { |
| 736 | error = -ENOTEMPTY; |
| 737 | goto out_gunlock; |
| 738 | } |
| 739 | } |
| 740 | } else { |
Miklos Szeredi | f58ba88 | 2008-07-02 21:12:01 +0200 | [diff] [blame] | 741 | error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 742 | if (error) |
| 743 | goto out_gunlock; |
| 744 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 745 | error = gfs2_dir_check(ndir, &ndentry->d_name, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 746 | switch (error) { |
| 747 | case -ENOENT: |
| 748 | error = 0; |
| 749 | break; |
| 750 | case 0: |
| 751 | error = -EEXIST; |
| 752 | default: |
| 753 | goto out_gunlock; |
| 754 | }; |
| 755 | |
| 756 | if (odip != ndip) { |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 757 | if (!ndip->i_inode.i_nlink) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 758 | error = -EINVAL; |
| 759 | goto out_gunlock; |
| 760 | } |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 761 | if (ndip->i_di.di_entries == (u32)-1) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 762 | error = -EFBIG; |
| 763 | goto out_gunlock; |
| 764 | } |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 765 | if (S_ISDIR(ip->i_inode.i_mode) && |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 766 | ndip->i_inode.i_nlink == (u32)-1) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 767 | error = -EMLINK; |
| 768 | goto out_gunlock; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /* Check out the dir to be renamed */ |
| 774 | |
| 775 | if (dir_rename) { |
Miklos Szeredi | f58ba88 | 2008-07-02 21:12:01 +0200 | [diff] [blame] | 776 | error = gfs2_permission(odentry->d_inode, MAY_WRITE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 777 | if (error) |
| 778 | goto out_gunlock; |
| 779 | } |
| 780 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 781 | alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name); |
| 782 | if (error < 0) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 783 | goto out_gunlock; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 784 | error = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 785 | |
| 786 | if (alloc_required) { |
| 787 | struct gfs2_alloc *al = gfs2_alloc_get(ndip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 788 | if (!al) { |
| 789 | error = -ENOMEM; |
| 790 | goto out_gunlock; |
| 791 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 792 | |
Steven Whitehouse | d82661d | 2008-03-10 15:34:50 +0000 | [diff] [blame] | 793 | error = gfs2_quota_lock_check(ndip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 794 | if (error) |
| 795 | goto out_alloc; |
| 796 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 797 | al->al_requested = sdp->sd_max_dirres; |
| 798 | |
| 799 | error = gfs2_inplace_reserve(ndip); |
| 800 | if (error) |
| 801 | goto out_gunlock_q; |
| 802 | |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame] | 803 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 804 | al->al_rgd->rd_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 805 | 4 * RES_DINODE + 4 * RES_LEAF + |
S. Wendy Cheng | 87d21e0 | 2007-01-18 16:07:03 -0500 | [diff] [blame] | 806 | RES_STATFS + RES_QUOTA + 4, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 807 | if (error) |
| 808 | goto out_ipreserv; |
| 809 | } else { |
| 810 | error = gfs2_trans_begin(sdp, 4 * RES_DINODE + |
S. Wendy Cheng | 87d21e0 | 2007-01-18 16:07:03 -0500 | [diff] [blame] | 811 | 5 * RES_LEAF + 4, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 812 | if (error) |
| 813 | goto out_gunlock; |
| 814 | } |
| 815 | |
| 816 | /* Remove the target file, if it exists */ |
| 817 | |
| 818 | if (nip) { |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 819 | if (S_ISDIR(nip->i_inode.i_mode)) |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 820 | error = gfs2_rmdiri(ndip, &ndentry->d_name, nip); |
| 821 | else { |
| 822 | error = gfs2_dir_del(ndip, &ndentry->d_name); |
| 823 | if (error) |
| 824 | goto out_end_trans; |
S. Wendy Cheng | 87d21e0 | 2007-01-18 16:07:03 -0500 | [diff] [blame] | 825 | error = gfs2_change_nlink(nip, -1); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 826 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 827 | if (error) |
| 828 | goto out_end_trans; |
| 829 | } |
| 830 | |
| 831 | if (dir_rename) { |
| 832 | struct qstr name; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 833 | gfs2_str2qstr(&name, ".."); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 834 | |
| 835 | error = gfs2_change_nlink(ndip, +1); |
| 836 | if (error) |
| 837 | goto out_end_trans; |
| 838 | error = gfs2_change_nlink(odip, -1); |
| 839 | if (error) |
| 840 | goto out_end_trans; |
| 841 | |
Steven Whitehouse | ffed8ab | 2007-06-07 11:29:35 +0100 | [diff] [blame] | 842 | error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 843 | if (error) |
| 844 | goto out_end_trans; |
| 845 | } else { |
| 846 | struct buffer_head *dibh; |
| 847 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 848 | if (error) |
| 849 | goto out_end_trans; |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 850 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 851 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 852 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 853 | brelse(dibh); |
| 854 | } |
| 855 | |
| 856 | error = gfs2_dir_del(odip, &odentry->d_name); |
| 857 | if (error) |
| 858 | goto out_end_trans; |
| 859 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 860 | error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 861 | if (error) |
| 862 | goto out_end_trans; |
| 863 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 864 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 865 | gfs2_trans_end(sdp); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 866 | out_ipreserv: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 867 | if (alloc_required) |
| 868 | gfs2_inplace_release(ndip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 869 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 870 | if (alloc_required) |
| 871 | gfs2_quota_unlock(ndip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 872 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 873 | if (alloc_required) |
| 874 | gfs2_alloc_put(ndip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 875 | out_gunlock: |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 876 | while (x--) { |
| 877 | gfs2_glock_dq(ghs + x); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 878 | gfs2_holder_uninit(ghs + x); |
Bob Peterson | 72dbf47 | 2008-08-12 13:39:29 -0500 | [diff] [blame] | 879 | } |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 880 | out_gunlock_r: |
Steven Whitehouse | 0188d6c | 2008-08-26 09:38:26 +0100 | [diff] [blame] | 881 | if (r_gh.gh_gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 882 | gfs2_glock_dq_uninit(&r_gh); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 883 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 884 | return error; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * gfs2_readlink - Read the value of a symlink |
| 889 | * @dentry: the symlink |
| 890 | * @buf: the buffer to read the symlink data into |
| 891 | * @size: the size of the buffer |
| 892 | * |
| 893 | * Returns: errno |
| 894 | */ |
| 895 | |
| 896 | static int gfs2_readlink(struct dentry *dentry, char __user *user_buf, |
| 897 | int user_size) |
| 898 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 899 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 900 | char array[GFS2_FAST_NAME_SIZE], *buf = array; |
| 901 | unsigned int len = GFS2_FAST_NAME_SIZE; |
| 902 | int error; |
| 903 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 904 | error = gfs2_readlinki(ip, &buf, &len); |
| 905 | if (error) |
| 906 | return error; |
| 907 | |
| 908 | if (user_size > len - 1) |
| 909 | user_size = len - 1; |
| 910 | |
| 911 | if (copy_to_user(user_buf, buf, user_size)) |
| 912 | error = -EFAULT; |
| 913 | else |
| 914 | error = user_size; |
| 915 | |
| 916 | if (buf != array) |
| 917 | kfree(buf); |
| 918 | |
| 919 | return error; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * gfs2_follow_link - Follow a symbolic link |
| 924 | * @dentry: The dentry of the link |
| 925 | * @nd: Data that we pass to vfs_follow_link() |
| 926 | * |
| 927 | * This can handle symlinks of any size. It is optimised for symlinks |
| 928 | * under GFS2_FAST_NAME_SIZE. |
| 929 | * |
| 930 | * Returns: 0 on success or error code |
| 931 | */ |
| 932 | |
| 933 | static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 934 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 935 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 936 | char array[GFS2_FAST_NAME_SIZE], *buf = array; |
| 937 | unsigned int len = GFS2_FAST_NAME_SIZE; |
| 938 | int error; |
| 939 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 940 | error = gfs2_readlinki(ip, &buf, &len); |
| 941 | if (!error) { |
| 942 | error = vfs_follow_link(nd, buf); |
| 943 | if (buf != array) |
| 944 | kfree(buf); |
| 945 | } |
| 946 | |
| 947 | return ERR_PTR(error); |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * gfs2_permission - |
| 952 | * @inode: |
| 953 | * @mask: |
| 954 | * @nd: passed from Linux VFS, ignored by us |
| 955 | * |
Steven Whitehouse | 300c7d7 | 2006-11-27 09:55:28 -0500 | [diff] [blame] | 956 | * This may be called from the VFS directly, or from within GFS2 with the |
| 957 | * inode locked, so we look to see if the glock is already locked and only |
| 958 | * lock the glock if its not already been done. |
| 959 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 960 | * Returns: errno |
| 961 | */ |
| 962 | |
Miklos Szeredi | f58ba88 | 2008-07-02 21:12:01 +0200 | [diff] [blame] | 963 | int gfs2_permission(struct inode *inode, int mask) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 964 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 965 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 966 | struct gfs2_holder i_gh; |
| 967 | int error; |
Steven Whitehouse | 300c7d7 | 2006-11-27 09:55:28 -0500 | [diff] [blame] | 968 | int unlock = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 969 | |
Steven Whitehouse | 7afd88d | 2008-02-22 16:07:18 +0000 | [diff] [blame] | 970 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
Steven Whitehouse | 300c7d7 | 2006-11-27 09:55:28 -0500 | [diff] [blame] | 971 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
| 972 | if (error) |
| 973 | return error; |
| 974 | unlock = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Miklos Szeredi | f58ba88 | 2008-07-02 21:12:01 +0200 | [diff] [blame] | 977 | if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode)) |
| 978 | error = -EACCES; |
| 979 | else |
| 980 | error = generic_permission(inode, mask, gfs2_check_acl); |
Steven Whitehouse | 300c7d7 | 2006-11-27 09:55:28 -0500 | [diff] [blame] | 981 | if (unlock) |
| 982 | gfs2_glock_dq_uninit(&i_gh); |
| 983 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 984 | return error; |
| 985 | } |
| 986 | |
| 987 | static int setattr_size(struct inode *inode, struct iattr *attr) |
| 988 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 989 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 990 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 991 | int error; |
| 992 | |
| 993 | if (attr->ia_size != ip->i_di.di_size) { |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 994 | error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 995 | if (error) |
| 996 | return error; |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 997 | error = vmtruncate(inode, attr->ia_size); |
| 998 | gfs2_trans_end(sdp); |
| 999 | if (error) |
| 1000 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Steven Whitehouse | aa6a85a | 2006-01-24 10:37:06 +0000 | [diff] [blame] | 1003 | error = gfs2_truncatei(ip, attr->ia_size); |
Wendy Cheng | 090ffaa | 2007-06-27 11:00:03 -0400 | [diff] [blame] | 1004 | if (error && (inode->i_size != ip->i_di.di_size)) |
| 1005 | i_size_write(inode, ip->i_di.di_size); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1006 | |
| 1007 | return error; |
| 1008 | } |
| 1009 | |
| 1010 | static int setattr_chown(struct inode *inode, struct iattr *attr) |
| 1011 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1012 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1013 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1014 | struct buffer_head *dibh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1015 | u32 ouid, ogid, nuid, ngid; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1016 | int error; |
| 1017 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 1018 | ouid = inode->i_uid; |
| 1019 | ogid = inode->i_gid; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1020 | nuid = attr->ia_uid; |
| 1021 | ngid = attr->ia_gid; |
| 1022 | |
| 1023 | if (!(attr->ia_valid & ATTR_UID) || ouid == nuid) |
| 1024 | ouid = nuid = NO_QUOTA_CHANGE; |
| 1025 | if (!(attr->ia_valid & ATTR_GID) || ogid == ngid) |
| 1026 | ogid = ngid = NO_QUOTA_CHANGE; |
| 1027 | |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 1028 | if (!gfs2_alloc_get(ip)) |
| 1029 | return -ENOMEM; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1030 | |
| 1031 | error = gfs2_quota_lock(ip, nuid, ngid); |
| 1032 | if (error) |
| 1033 | goto out_alloc; |
| 1034 | |
| 1035 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { |
| 1036 | error = gfs2_quota_check(ip, nuid, ngid); |
| 1037 | if (error) |
| 1038 | goto out_gunlock_q; |
| 1039 | } |
| 1040 | |
| 1041 | error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0); |
| 1042 | if (error) |
| 1043 | goto out_gunlock_q; |
| 1044 | |
| 1045 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1046 | if (error) |
| 1047 | goto out_end_trans; |
| 1048 | |
| 1049 | error = inode_setattr(inode, attr); |
| 1050 | gfs2_assert_warn(sdp, !error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1051 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1052 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1053 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1054 | brelse(dibh); |
| 1055 | |
| 1056 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 1057 | u64 blocks = gfs2_get_inode_blocks(&ip->i_inode); |
| 1058 | gfs2_quota_change(ip, -blocks, ouid, ogid); |
| 1059 | gfs2_quota_change(ip, blocks, nuid, ngid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1062 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1063 | gfs2_trans_end(sdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1064 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1065 | gfs2_quota_unlock(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1066 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1067 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1068 | return error; |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * gfs2_setattr - Change attributes on an inode |
| 1073 | * @dentry: The dentry which is changing |
| 1074 | * @attr: The structure describing the change |
| 1075 | * |
| 1076 | * The VFS layer wants to change one or more of an inodes attributes. Write |
| 1077 | * that change out to disk. |
| 1078 | * |
| 1079 | * Returns: errno |
| 1080 | */ |
| 1081 | |
| 1082 | static int gfs2_setattr(struct dentry *dentry, struct iattr *attr) |
| 1083 | { |
| 1084 | struct inode *inode = dentry->d_inode; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1085 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1086 | struct gfs2_holder i_gh; |
| 1087 | int error; |
| 1088 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1089 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 1090 | if (error) |
| 1091 | return error; |
| 1092 | |
| 1093 | error = -EPERM; |
| 1094 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 1095 | goto out; |
| 1096 | |
| 1097 | error = inode_change_ok(inode, attr); |
| 1098 | if (error) |
| 1099 | goto out; |
| 1100 | |
| 1101 | if (attr->ia_valid & ATTR_SIZE) |
| 1102 | error = setattr_size(inode, attr); |
| 1103 | else if (attr->ia_valid & (ATTR_UID | ATTR_GID)) |
| 1104 | error = setattr_chown(inode, attr); |
| 1105 | else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode)) |
| 1106 | error = gfs2_acl_chmod(ip, attr); |
| 1107 | else |
| 1108 | error = gfs2_setattr_simple(ip, attr); |
| 1109 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1110 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1111 | gfs2_glock_dq_uninit(&i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1112 | if (!error) |
| 1113 | mark_inode_dirty(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1114 | return error; |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * gfs2_getattr - Read out an inode's attributes |
Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 1119 | * @mnt: The vfsmount the inode is being accessed from |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1120 | * @dentry: The dentry to stat |
| 1121 | * @stat: The inode's stats |
| 1122 | * |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1123 | * This may be called from the VFS directly, or from within GFS2 with the |
| 1124 | * inode locked, so we look to see if the glock is already locked and only |
| 1125 | * lock the glock if its not already been done. Note that its the NFS |
| 1126 | * readdirplus operation which causes this to be called (from filldir) |
| 1127 | * with the glock already held. |
| 1128 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1129 | * Returns: errno |
| 1130 | */ |
| 1131 | |
| 1132 | static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 1133 | struct kstat *stat) |
| 1134 | { |
| 1135 | struct inode *inode = dentry->d_inode; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1136 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1137 | struct gfs2_holder gh; |
| 1138 | int error; |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1139 | int unlock = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1140 | |
Steven Whitehouse | 7afd88d | 2008-02-22 16:07:18 +0000 | [diff] [blame] | 1141 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1142 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
| 1143 | if (error) |
| 1144 | return error; |
| 1145 | unlock = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1148 | generic_fillattr(inode, stat); |
Steven Whitehouse | d7c103d | 2007-01-25 17:14:59 +0000 | [diff] [blame] | 1149 | if (unlock) |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1150 | gfs2_glock_dq_uninit(&gh); |
| 1151 | |
| 1152 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | static int gfs2_setxattr(struct dentry *dentry, const char *name, |
| 1156 | const void *data, size_t size, int flags) |
| 1157 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1158 | struct inode *inode = dentry->d_inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1159 | struct gfs2_ea_request er; |
| 1160 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1161 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 1162 | er.er_type = gfs2_ea_name2type(name, &er.er_name); |
| 1163 | if (er.er_type == GFS2_EATYPE_UNUSED) |
| 1164 | return -EOPNOTSUPP; |
| 1165 | er.er_data = (char *)data; |
| 1166 | er.er_name_len = strlen(er.er_name); |
| 1167 | er.er_data_len = size; |
| 1168 | er.er_flags = flags; |
| 1169 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1170 | gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1171 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1172 | return gfs2_ea_set(GFS2_I(inode), &er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name, |
| 1176 | void *data, size_t size) |
| 1177 | { |
| 1178 | struct gfs2_ea_request er; |
| 1179 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1180 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 1181 | er.er_type = gfs2_ea_name2type(name, &er.er_name); |
| 1182 | if (er.er_type == GFS2_EATYPE_UNUSED) |
| 1183 | return -EOPNOTSUPP; |
| 1184 | er.er_data = data; |
| 1185 | er.er_name_len = strlen(er.er_name); |
| 1186 | er.er_data_len = size; |
| 1187 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1188 | return gfs2_ea_get(GFS2_I(dentry->d_inode), &er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size) |
| 1192 | { |
| 1193 | struct gfs2_ea_request er; |
| 1194 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1195 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 1196 | er.er_data = (size) ? buffer : NULL; |
| 1197 | er.er_data_len = size; |
| 1198 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1199 | return gfs2_ea_list(GFS2_I(dentry->d_inode), &er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static int gfs2_removexattr(struct dentry *dentry, const char *name) |
| 1203 | { |
| 1204 | struct gfs2_ea_request er; |
| 1205 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1206 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 1207 | er.er_type = gfs2_ea_name2type(name, &er.er_name); |
| 1208 | if (er.er_type == GFS2_EATYPE_UNUSED) |
| 1209 | return -EOPNOTSUPP; |
| 1210 | er.er_name_len = strlen(er.er_name); |
| 1211 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1212 | return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1215 | const struct inode_operations gfs2_file_iops = { |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 1216 | .permission = gfs2_permission, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1217 | .setattr = gfs2_setattr, |
| 1218 | .getattr = gfs2_getattr, |
| 1219 | .setxattr = gfs2_setxattr, |
| 1220 | .getxattr = gfs2_getxattr, |
| 1221 | .listxattr = gfs2_listxattr, |
| 1222 | .removexattr = gfs2_removexattr, |
| 1223 | }; |
| 1224 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1225 | const struct inode_operations gfs2_dir_iops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1226 | .create = gfs2_create, |
| 1227 | .lookup = gfs2_lookup, |
| 1228 | .link = gfs2_link, |
| 1229 | .unlink = gfs2_unlink, |
| 1230 | .symlink = gfs2_symlink, |
| 1231 | .mkdir = gfs2_mkdir, |
| 1232 | .rmdir = gfs2_rmdir, |
| 1233 | .mknod = gfs2_mknod, |
| 1234 | .rename = gfs2_rename, |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 1235 | .permission = gfs2_permission, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1236 | .setattr = gfs2_setattr, |
| 1237 | .getattr = gfs2_getattr, |
| 1238 | .setxattr = gfs2_setxattr, |
| 1239 | .getxattr = gfs2_getxattr, |
| 1240 | .listxattr = gfs2_listxattr, |
| 1241 | .removexattr = gfs2_removexattr, |
| 1242 | }; |
| 1243 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1244 | const struct inode_operations gfs2_symlink_iops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1245 | .readlink = gfs2_readlink, |
| 1246 | .follow_link = gfs2_follow_link, |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 1247 | .permission = gfs2_permission, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1248 | .setattr = gfs2_setattr, |
| 1249 | .getattr = gfs2_getattr, |
| 1250 | .setxattr = gfs2_setxattr, |
| 1251 | .getxattr = gfs2_getxattr, |
| 1252 | .listxattr = gfs2_listxattr, |
| 1253 | .removexattr = gfs2_removexattr, |
| 1254 | }; |
| 1255 | |