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