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