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