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