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