David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 10 | #include <linux/slab.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/completion.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/namei.h> |
| 15 | #include <linux/utsname.h> |
| 16 | #include <linux/mm.h> |
| 17 | #include <linux/xattr.h> |
| 18 | #include <linux/posix_acl.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 20 | #include <linux/crc32.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 21 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #include <asm/uaccess.h> |
| 23 | |
| 24 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 25 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 26 | #include "acl.h" |
| 27 | #include "bmap.h" |
| 28 | #include "dir.h" |
| 29 | #include "eaops.h" |
| 30 | #include "eattr.h" |
| 31 | #include "glock.h" |
| 32 | #include "inode.h" |
| 33 | #include "meta_io.h" |
| 34 | #include "ops_dentry.h" |
| 35 | #include "ops_inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | #include "quota.h" |
| 37 | #include "rgrp.h" |
| 38 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 39 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * gfs2_create - Create a file |
| 43 | * @dir: The directory in which to create the file |
| 44 | * @dentry: The dentry of the new file |
| 45 | * @mode: The mode of the new file |
| 46 | * |
| 47 | * Returns: errno |
| 48 | */ |
| 49 | |
| 50 | static int gfs2_create(struct inode *dir, struct dentry *dentry, |
| 51 | int mode, struct nameidata *nd) |
| 52 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 53 | struct gfs2_inode *dip = GFS2_I(dir); |
| 54 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 55 | struct gfs2_holder ghs[2]; |
| 56 | struct inode *inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 57 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 58 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 59 | |
| 60 | for (;;) { |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 61 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 62 | if (!IS_ERR(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 63 | gfs2_trans_end(sdp); |
| 64 | if (dip->i_alloc.al_rgd) |
| 65 | gfs2_inplace_release(dip); |
| 66 | gfs2_quota_unlock(dip); |
| 67 | gfs2_alloc_put(dip); |
| 68 | gfs2_glock_dq_uninit_m(2, ghs); |
Steven Whitehouse | 3a8476d | 2006-06-19 09:10:39 -0400 | [diff] [blame] | 69 | mark_inode_dirty(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 70 | break; |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 71 | } else if (PTR_ERR(inode) != -EEXIST || |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 72 | (nd->intent.open.flags & O_EXCL)) { |
| 73 | gfs2_holder_uninit(ghs); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 74 | return PTR_ERR(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 77 | inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd); |
| 78 | if (inode) { |
| 79 | if (!IS_ERR(inode)) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 80 | gfs2_holder_uninit(ghs); |
| 81 | break; |
| 82 | } else { |
| 83 | gfs2_holder_uninit(ghs); |
| 84 | return PTR_ERR(inode); |
| 85 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 89 | d_instantiate(dentry, inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * gfs2_lookup - Look up a filename in a directory and return its inode |
| 96 | * @dir: The directory inode |
| 97 | * @dentry: The dentry of the new inode |
| 98 | * @nd: passed from Linux VFS, ignored by us |
| 99 | * |
| 100 | * Called by the VFS layer. Lock dir and call gfs2_lookupi() |
| 101 | * |
| 102 | * Returns: errno |
| 103 | */ |
| 104 | |
| 105 | static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry, |
| 106 | struct nameidata *nd) |
| 107 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 108 | struct inode *inode = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 109 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 110 | dentry->d_op = &gfs2_dops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 111 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 112 | inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd); |
| 113 | if (inode && IS_ERR(inode)) |
| 114 | return ERR_PTR(PTR_ERR(inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 115 | |
| 116 | if (inode) |
| 117 | return d_splice_alias(inode, dentry); |
| 118 | d_add(dentry, inode); |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * gfs2_link - Link to a file |
| 125 | * @old_dentry: The inode to link |
| 126 | * @dir: Add link to this directory |
| 127 | * @dentry: The name of the link |
| 128 | * |
| 129 | * Link the inode in "old_dentry" into the directory "dir" with the |
| 130 | * name in "dentry". |
| 131 | * |
| 132 | * Returns: errno |
| 133 | */ |
| 134 | |
| 135 | static int gfs2_link(struct dentry *old_dentry, struct inode *dir, |
| 136 | struct dentry *dentry) |
| 137 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 138 | struct gfs2_inode *dip = GFS2_I(dir); |
| 139 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 140 | struct inode *inode = old_dentry->d_inode; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 141 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 142 | struct gfs2_holder ghs[2]; |
| 143 | int alloc_required; |
| 144 | int error; |
| 145 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 146 | if (S_ISDIR(inode->i_mode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 147 | return -EPERM; |
| 148 | |
| 149 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 150 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 151 | |
| 152 | error = gfs2_glock_nq_m(2, ghs); |
| 153 | if (error) |
| 154 | goto out; |
| 155 | |
Steven Whitehouse | faf450e | 2006-06-22 10:59:10 -0400 | [diff] [blame] | 156 | error = permission(dir, MAY_WRITE | MAY_EXEC, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 157 | if (error) |
| 158 | goto out_gunlock; |
| 159 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 160 | error = gfs2_dir_check(dir, &dentry->d_name, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 161 | switch (error) { |
| 162 | case -ENOENT: |
| 163 | break; |
| 164 | case 0: |
| 165 | error = -EEXIST; |
| 166 | default: |
| 167 | goto out_gunlock; |
| 168 | } |
| 169 | |
| 170 | error = -EINVAL; |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 171 | if (!dip->i_inode.i_nlink) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 172 | goto out_gunlock; |
| 173 | error = -EFBIG; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 174 | if (dip->i_di.di_entries == (u32)-1) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 175 | goto out_gunlock; |
| 176 | error = -EPERM; |
| 177 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 178 | goto out_gunlock; |
| 179 | error = -EINVAL; |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 180 | if (!ip->i_inode.i_nlink) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 181 | goto out_gunlock; |
| 182 | error = -EMLINK; |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 183 | if (ip->i_inode.i_nlink == (u32)-1) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 184 | goto out_gunlock; |
| 185 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 186 | alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name); |
| 187 | if (error < 0) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 188 | goto out_gunlock; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 189 | error = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 190 | |
| 191 | if (alloc_required) { |
| 192 | struct gfs2_alloc *al = gfs2_alloc_get(dip); |
| 193 | |
| 194 | error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 195 | if (error) |
| 196 | goto out_alloc; |
| 197 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 198 | error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 199 | if (error) |
| 200 | goto out_gunlock_q; |
| 201 | |
| 202 | al->al_requested = sdp->sd_max_dirres; |
| 203 | |
| 204 | error = gfs2_inplace_reserve(dip); |
| 205 | if (error) |
| 206 | goto out_gunlock_q; |
| 207 | |
Steven Whitehouse | 1b50259 | 2006-05-18 14:10:52 -0400 | [diff] [blame] | 208 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 209 | al->al_rgd->rd_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 210 | 2 * RES_DINODE + RES_STATFS + |
| 211 | RES_QUOTA, 0); |
| 212 | if (error) |
| 213 | goto out_ipres; |
| 214 | } else { |
| 215 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0); |
| 216 | if (error) |
| 217 | goto out_ipres; |
| 218 | } |
| 219 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 220 | 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] | 221 | if (error) |
| 222 | goto out_end_trans; |
| 223 | |
| 224 | error = gfs2_change_nlink(ip, +1); |
| 225 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 226 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 227 | gfs2_trans_end(sdp); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 228 | out_ipres: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 229 | if (alloc_required) |
| 230 | gfs2_inplace_release(dip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 231 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 232 | if (alloc_required) |
| 233 | gfs2_quota_unlock(dip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 234 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 235 | if (alloc_required) |
| 236 | gfs2_alloc_put(dip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 237 | out_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 238 | gfs2_glock_dq_m(2, ghs); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 239 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 240 | gfs2_holder_uninit(ghs); |
| 241 | gfs2_holder_uninit(ghs + 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 242 | if (!error) { |
Steven Whitehouse | 29937ac | 2006-07-06 17:58:03 -0400 | [diff] [blame] | 243 | atomic_inc(&inode->i_count); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 244 | d_instantiate(dentry, inode); |
| 245 | mark_inode_dirty(inode); |
| 246 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 247 | return error; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * gfs2_unlink - Unlink a file |
| 252 | * @dir: The inode of the directory containing the file to unlink |
| 253 | * @dentry: The file itself |
| 254 | * |
| 255 | * Unlink a file. Call gfs2_unlinki() |
| 256 | * |
| 257 | * Returns: errno |
| 258 | */ |
| 259 | |
| 260 | static int gfs2_unlink(struct inode *dir, struct dentry *dentry) |
| 261 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 262 | struct gfs2_inode *dip = GFS2_I(dir); |
| 263 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 264 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 265 | struct gfs2_holder ghs[3]; |
| 266 | struct gfs2_rgrpd *rgd; |
| 267 | struct gfs2_holder ri_gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 268 | int error; |
| 269 | |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 270 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 271 | if (error) |
| 272 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 273 | |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 274 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 275 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 276 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 277 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 278 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
| 279 | |
| 280 | |
| 281 | error = gfs2_glock_nq_m(3, ghs); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 282 | if (error) |
| 283 | goto out; |
| 284 | |
| 285 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); |
| 286 | if (error) |
| 287 | goto out_gunlock; |
| 288 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 289 | 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] | 290 | if (error) |
| 291 | goto out_gunlock; |
| 292 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 293 | error = gfs2_dir_del(dip, &dentry->d_name); |
| 294 | if (error) |
| 295 | goto out_end_trans; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 296 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 297 | error = gfs2_change_nlink(ip, -1); |
| 298 | |
| 299 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 300 | gfs2_trans_end(sdp); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 301 | out_gunlock: |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 302 | gfs2_glock_dq_m(3, ghs); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 303 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 304 | gfs2_holder_uninit(ghs); |
| 305 | gfs2_holder_uninit(ghs + 1); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 306 | gfs2_holder_uninit(ghs + 2); |
| 307 | gfs2_glock_dq_uninit(&ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 308 | return error; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * gfs2_symlink - Create a symlink |
| 313 | * @dir: The directory to create the symlink in |
| 314 | * @dentry: The dentry to put the symlink in |
| 315 | * @symname: The thing which the link points to |
| 316 | * |
| 317 | * Returns: errno |
| 318 | */ |
| 319 | |
| 320 | static int gfs2_symlink(struct inode *dir, struct dentry *dentry, |
| 321 | const char *symname) |
| 322 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 323 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
| 324 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 325 | struct gfs2_holder ghs[2]; |
| 326 | struct inode *inode; |
| 327 | struct buffer_head *dibh; |
| 328 | int size; |
| 329 | int error; |
| 330 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 331 | /* Must be stuffed with a null terminator for gfs2_follow_link() */ |
| 332 | size = strlen(symname); |
| 333 | if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1) |
| 334 | return -ENAMETOOLONG; |
| 335 | |
| 336 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 337 | |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 338 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 339 | if (IS_ERR(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 340 | gfs2_holder_uninit(ghs); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 341 | return PTR_ERR(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 344 | ip = ghs[1].gh_gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 345 | |
| 346 | ip->i_di.di_size = size; |
| 347 | |
| 348 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 349 | |
| 350 | if (!gfs2_assert_withdraw(sdp, !error)) { |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 351 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 352 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, |
| 353 | size); |
| 354 | brelse(dibh); |
| 355 | } |
| 356 | |
| 357 | gfs2_trans_end(sdp); |
| 358 | if (dip->i_alloc.al_rgd) |
| 359 | gfs2_inplace_release(dip); |
| 360 | gfs2_quota_unlock(dip); |
| 361 | gfs2_alloc_put(dip); |
| 362 | |
| 363 | gfs2_glock_dq_uninit_m(2, ghs); |
| 364 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 365 | d_instantiate(dentry, inode); |
| 366 | mark_inode_dirty(inode); |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * gfs2_mkdir - Make a directory |
| 373 | * @dir: The parent directory of the new one |
| 374 | * @dentry: The dentry of the new directory |
| 375 | * @mode: The mode of the new directory |
| 376 | * |
| 377 | * Returns: errno |
| 378 | */ |
| 379 | |
| 380 | static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 381 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 382 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
| 383 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 384 | struct gfs2_holder ghs[2]; |
| 385 | struct inode *inode; |
| 386 | struct buffer_head *dibh; |
| 387 | int error; |
| 388 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 389 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 390 | |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 391 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 392 | if (IS_ERR(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 393 | gfs2_holder_uninit(ghs); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 394 | return PTR_ERR(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 397 | ip = ghs[1].gh_gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 398 | |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 399 | ip->i_inode.i_nlink = 2; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 400 | ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode); |
| 401 | ip->i_di.di_flags |= GFS2_DIF_JDATA; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 402 | ip->i_di.di_entries = 2; |
| 403 | |
| 404 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 405 | |
| 406 | if (!gfs2_assert_withdraw(sdp, !error)) { |
| 407 | struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 408 | struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 409 | struct qstr str; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 410 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 411 | gfs2_str2qstr(&str, "."); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 412 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 413 | gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 414 | dent->de_inum = di->di_num; /* already GFS2 endian */ |
Steven Whitehouse | 7ecdb70 | 2006-10-03 21:03:35 -0400 | [diff] [blame] | 415 | dent->de_type = cpu_to_be16(DT_DIR); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 416 | di->di_entries = cpu_to_be32(1); |
| 417 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 418 | gfs2_str2qstr(&str, ".."); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 419 | dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1)); |
| 420 | 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] | 421 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 422 | gfs2_inum_out(dip, dent); |
Steven Whitehouse | 7ecdb70 | 2006-10-03 21:03:35 -0400 | [diff] [blame] | 423 | dent->de_type = cpu_to_be16(DT_DIR); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 424 | |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 425 | gfs2_dinode_out(ip, di); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 426 | |
| 427 | brelse(dibh); |
| 428 | } |
| 429 | |
| 430 | error = gfs2_change_nlink(dip, +1); |
| 431 | gfs2_assert_withdraw(sdp, !error); /* dip already pinned */ |
| 432 | |
| 433 | gfs2_trans_end(sdp); |
| 434 | if (dip->i_alloc.al_rgd) |
| 435 | gfs2_inplace_release(dip); |
| 436 | gfs2_quota_unlock(dip); |
| 437 | gfs2_alloc_put(dip); |
| 438 | |
| 439 | gfs2_glock_dq_uninit_m(2, ghs); |
| 440 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 441 | d_instantiate(dentry, inode); |
| 442 | mark_inode_dirty(inode); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * gfs2_rmdir - Remove a directory |
| 449 | * @dir: The parent directory of the directory to be removed |
| 450 | * @dentry: The dentry of the directory to remove |
| 451 | * |
| 452 | * Remove a directory. Call gfs2_rmdiri() |
| 453 | * |
| 454 | * Returns: errno |
| 455 | */ |
| 456 | |
| 457 | static int gfs2_rmdir(struct inode *dir, struct dentry *dentry) |
| 458 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 459 | struct gfs2_inode *dip = GFS2_I(dir); |
| 460 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
| 461 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 462 | struct gfs2_holder ghs[3]; |
| 463 | struct gfs2_rgrpd *rgd; |
| 464 | struct gfs2_holder ri_gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 465 | int error; |
| 466 | |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 467 | |
| 468 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 469 | if (error) |
| 470 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 471 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
| 472 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
| 473 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 474 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 475 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
| 476 | |
| 477 | error = gfs2_glock_nq_m(3, ghs); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 478 | if (error) |
| 479 | goto out; |
| 480 | |
| 481 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); |
| 482 | if (error) |
| 483 | goto out_gunlock; |
| 484 | |
| 485 | if (ip->i_di.di_entries < 2) { |
| 486 | if (gfs2_consist_inode(ip)) |
Steven Whitehouse | 4cc14f0 | 2006-10-31 19:00:24 -0500 | [diff] [blame] | 487 | gfs2_dinode_print(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 488 | error = -EIO; |
| 489 | goto out_gunlock; |
| 490 | } |
| 491 | if (ip->i_di.di_entries > 2) { |
| 492 | error = -ENOTEMPTY; |
| 493 | goto out_gunlock; |
| 494 | } |
| 495 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 496 | 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] | 497 | if (error) |
| 498 | goto out_gunlock; |
| 499 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 500 | error = gfs2_rmdiri(dip, &dentry->d_name, ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 501 | |
| 502 | gfs2_trans_end(sdp); |
| 503 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 504 | out_gunlock: |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 505 | gfs2_glock_dq_m(3, ghs); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 506 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 507 | gfs2_holder_uninit(ghs); |
| 508 | gfs2_holder_uninit(ghs + 1); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 509 | gfs2_holder_uninit(ghs + 2); |
| 510 | gfs2_glock_dq_uninit(&ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 511 | return error; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * gfs2_mknod - Make a special file |
| 516 | * @dir: The directory in which the special file will reside |
| 517 | * @dentry: The dentry of the special file |
| 518 | * @mode: The mode of the special file |
| 519 | * @rdev: The device specification of the special file |
| 520 | * |
| 521 | */ |
| 522 | |
| 523 | static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 524 | dev_t dev) |
| 525 | { |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 526 | struct gfs2_inode *dip = GFS2_I(dir); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 527 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 528 | struct gfs2_holder ghs[2]; |
| 529 | struct inode *inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 530 | |
| 531 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
| 532 | |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 533 | inode = gfs2_createi(ghs, &dentry->d_name, mode, dev); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 534 | if (IS_ERR(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 535 | gfs2_holder_uninit(ghs); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 536 | return PTR_ERR(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 537 | } |
| 538 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 539 | gfs2_trans_end(sdp); |
| 540 | if (dip->i_alloc.al_rgd) |
| 541 | gfs2_inplace_release(dip); |
| 542 | gfs2_quota_unlock(dip); |
| 543 | gfs2_alloc_put(dip); |
| 544 | |
| 545 | gfs2_glock_dq_uninit_m(2, ghs); |
| 546 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 547 | d_instantiate(dentry, inode); |
| 548 | mark_inode_dirty(inode); |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * gfs2_rename - Rename a file |
| 555 | * @odir: Parent directory of old file name |
| 556 | * @odentry: The old dentry of the file |
| 557 | * @ndir: Parent directory of new file name |
| 558 | * @ndentry: The new dentry of the file |
| 559 | * |
| 560 | * Returns: errno |
| 561 | */ |
| 562 | |
| 563 | static int gfs2_rename(struct inode *odir, struct dentry *odentry, |
| 564 | struct inode *ndir, struct dentry *ndentry) |
| 565 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 566 | struct gfs2_inode *odip = GFS2_I(odir); |
| 567 | struct gfs2_inode *ndip = GFS2_I(ndir); |
| 568 | struct gfs2_inode *ip = GFS2_I(odentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 569 | struct gfs2_inode *nip = NULL; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 570 | struct gfs2_sbd *sdp = GFS2_SB(odir); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 571 | struct gfs2_holder ghs[5], r_gh; |
| 572 | struct gfs2_rgrpd *nrgd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 573 | unsigned int num_gh; |
| 574 | int dir_rename = 0; |
| 575 | int alloc_required; |
| 576 | unsigned int x; |
| 577 | int error; |
| 578 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 579 | if (ndentry->d_inode) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 580 | nip = GFS2_I(ndentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 581 | if (ip == nip) |
| 582 | return 0; |
| 583 | } |
| 584 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 585 | /* Make sure we aren't trying to move a dirctory into it's subdir */ |
| 586 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 587 | if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 588 | dir_rename = 1; |
| 589 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 590 | error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 591 | &r_gh); |
| 592 | if (error) |
| 593 | goto out; |
| 594 | |
| 595 | error = gfs2_ok_to_move(ip, ndip); |
| 596 | if (error) |
| 597 | goto out_gunlock_r; |
| 598 | } |
| 599 | |
Steven Whitehouse | d9d1ca3 | 2006-06-21 15:38:17 -0400 | [diff] [blame] | 600 | num_gh = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 601 | gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
Steven Whitehouse | d9d1ca3 | 2006-06-21 15:38:17 -0400 | [diff] [blame] | 602 | if (odip != ndip) { |
| 603 | gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 604 | num_gh++; |
| 605 | } |
| 606 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 607 | num_gh++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 608 | |
Steven Whitehouse | d9d1ca3 | 2006-06-21 15:38:17 -0400 | [diff] [blame] | 609 | if (nip) { |
| 610 | gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); |
| 611 | num_gh++; |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 612 | /* grab the resource lock for unlink flag twiddling |
| 613 | * this is the case of the target file already existing |
| 614 | * so we unlink before doing the rename |
| 615 | */ |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 616 | nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr); |
Russell Cattelan | ddee760 | 2007-01-29 17:13:44 -0600 | [diff] [blame] | 617 | if (nrgd) |
| 618 | 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] | 619 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 620 | |
| 621 | error = gfs2_glock_nq_m(num_gh, ghs); |
| 622 | if (error) |
| 623 | goto out_uninit; |
| 624 | |
| 625 | /* Check out the old directory */ |
| 626 | |
| 627 | error = gfs2_unlink_ok(odip, &odentry->d_name, ip); |
| 628 | if (error) |
| 629 | goto out_gunlock; |
| 630 | |
| 631 | /* Check out the new directory */ |
| 632 | |
| 633 | if (nip) { |
| 634 | error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); |
| 635 | if (error) |
| 636 | goto out_gunlock; |
| 637 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 638 | if (S_ISDIR(nip->i_inode.i_mode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 639 | if (nip->i_di.di_entries < 2) { |
| 640 | if (gfs2_consist_inode(nip)) |
Steven Whitehouse | 4cc14f0 | 2006-10-31 19:00:24 -0500 | [diff] [blame] | 641 | gfs2_dinode_print(nip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 642 | error = -EIO; |
| 643 | goto out_gunlock; |
| 644 | } |
| 645 | if (nip->i_di.di_entries > 2) { |
| 646 | error = -ENOTEMPTY; |
| 647 | goto out_gunlock; |
| 648 | } |
| 649 | } |
| 650 | } else { |
Steven Whitehouse | faf450e | 2006-06-22 10:59:10 -0400 | [diff] [blame] | 651 | error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 652 | if (error) |
| 653 | goto out_gunlock; |
| 654 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 655 | error = gfs2_dir_check(ndir, &ndentry->d_name, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 656 | switch (error) { |
| 657 | case -ENOENT: |
| 658 | error = 0; |
| 659 | break; |
| 660 | case 0: |
| 661 | error = -EEXIST; |
| 662 | default: |
| 663 | goto out_gunlock; |
| 664 | }; |
| 665 | |
| 666 | if (odip != ndip) { |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 667 | if (!ndip->i_inode.i_nlink) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 668 | error = -EINVAL; |
| 669 | goto out_gunlock; |
| 670 | } |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 671 | if (ndip->i_di.di_entries == (u32)-1) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 672 | error = -EFBIG; |
| 673 | goto out_gunlock; |
| 674 | } |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 675 | if (S_ISDIR(ip->i_inode.i_mode) && |
Steven Whitehouse | 4f56110 | 2006-11-01 14:04:17 -0500 | [diff] [blame] | 676 | ndip->i_inode.i_nlink == (u32)-1) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 677 | error = -EMLINK; |
| 678 | goto out_gunlock; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* Check out the dir to be renamed */ |
| 684 | |
| 685 | if (dir_rename) { |
Steven Whitehouse | faf450e | 2006-06-22 10:59:10 -0400 | [diff] [blame] | 686 | error = permission(odentry->d_inode, MAY_WRITE, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 687 | if (error) |
| 688 | goto out_gunlock; |
| 689 | } |
| 690 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 691 | alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name); |
| 692 | if (error < 0) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 693 | goto out_gunlock; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 694 | error = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 695 | |
| 696 | if (alloc_required) { |
| 697 | struct gfs2_alloc *al = gfs2_alloc_get(ndip); |
| 698 | |
| 699 | error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 700 | if (error) |
| 701 | goto out_alloc; |
| 702 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 703 | error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 704 | if (error) |
| 705 | goto out_gunlock_q; |
| 706 | |
| 707 | al->al_requested = sdp->sd_max_dirres; |
| 708 | |
| 709 | error = gfs2_inplace_reserve(ndip); |
| 710 | if (error) |
| 711 | goto out_gunlock_q; |
| 712 | |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame] | 713 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 714 | al->al_rgd->rd_length + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 715 | 4 * RES_DINODE + 4 * RES_LEAF + |
S. Wendy Cheng | 87d21e0 | 2007-01-18 16:07:03 -0500 | [diff] [blame] | 716 | RES_STATFS + RES_QUOTA + 4, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 717 | if (error) |
| 718 | goto out_ipreserv; |
| 719 | } else { |
| 720 | error = gfs2_trans_begin(sdp, 4 * RES_DINODE + |
S. Wendy Cheng | 87d21e0 | 2007-01-18 16:07:03 -0500 | [diff] [blame] | 721 | 5 * RES_LEAF + 4, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 722 | if (error) |
| 723 | goto out_gunlock; |
| 724 | } |
| 725 | |
| 726 | /* Remove the target file, if it exists */ |
| 727 | |
| 728 | if (nip) { |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 729 | if (S_ISDIR(nip->i_inode.i_mode)) |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 730 | error = gfs2_rmdiri(ndip, &ndentry->d_name, nip); |
| 731 | else { |
| 732 | error = gfs2_dir_del(ndip, &ndentry->d_name); |
| 733 | if (error) |
| 734 | goto out_end_trans; |
S. Wendy Cheng | 87d21e0 | 2007-01-18 16:07:03 -0500 | [diff] [blame] | 735 | error = gfs2_change_nlink(nip, -1); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 736 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 737 | if (error) |
| 738 | goto out_end_trans; |
| 739 | } |
| 740 | |
| 741 | if (dir_rename) { |
| 742 | struct qstr name; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 743 | gfs2_str2qstr(&name, ".."); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 744 | |
| 745 | error = gfs2_change_nlink(ndip, +1); |
| 746 | if (error) |
| 747 | goto out_end_trans; |
| 748 | error = gfs2_change_nlink(odip, -1); |
| 749 | if (error) |
| 750 | goto out_end_trans; |
| 751 | |
Steven Whitehouse | ffed8ab | 2007-06-07 11:29:35 +0100 | [diff] [blame^] | 752 | error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 753 | if (error) |
| 754 | goto out_end_trans; |
| 755 | } else { |
| 756 | struct buffer_head *dibh; |
| 757 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 758 | if (error) |
| 759 | goto out_end_trans; |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 760 | ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 761 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 762 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 763 | brelse(dibh); |
| 764 | } |
| 765 | |
| 766 | error = gfs2_dir_del(odip, &odentry->d_name); |
| 767 | if (error) |
| 768 | goto out_end_trans; |
| 769 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 770 | 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] | 771 | if (error) |
| 772 | goto out_end_trans; |
| 773 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 774 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 775 | gfs2_trans_end(sdp); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 776 | out_ipreserv: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 777 | if (alloc_required) |
| 778 | gfs2_inplace_release(ndip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 779 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 780 | if (alloc_required) |
| 781 | gfs2_quota_unlock(ndip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 782 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 783 | if (alloc_required) |
| 784 | gfs2_alloc_put(ndip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 785 | out_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 786 | gfs2_glock_dq_m(num_gh, ghs); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 787 | out_uninit: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 788 | for (x = 0; x < num_gh; x++) |
| 789 | gfs2_holder_uninit(ghs + x); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 790 | out_gunlock_r: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 791 | if (dir_rename) |
| 792 | gfs2_glock_dq_uninit(&r_gh); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 793 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 794 | return error; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * gfs2_readlink - Read the value of a symlink |
| 799 | * @dentry: the symlink |
| 800 | * @buf: the buffer to read the symlink data into |
| 801 | * @size: the size of the buffer |
| 802 | * |
| 803 | * Returns: errno |
| 804 | */ |
| 805 | |
| 806 | static int gfs2_readlink(struct dentry *dentry, char __user *user_buf, |
| 807 | int user_size) |
| 808 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 809 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 810 | char array[GFS2_FAST_NAME_SIZE], *buf = array; |
| 811 | unsigned int len = GFS2_FAST_NAME_SIZE; |
| 812 | int error; |
| 813 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 814 | error = gfs2_readlinki(ip, &buf, &len); |
| 815 | if (error) |
| 816 | return error; |
| 817 | |
| 818 | if (user_size > len - 1) |
| 819 | user_size = len - 1; |
| 820 | |
| 821 | if (copy_to_user(user_buf, buf, user_size)) |
| 822 | error = -EFAULT; |
| 823 | else |
| 824 | error = user_size; |
| 825 | |
| 826 | if (buf != array) |
| 827 | kfree(buf); |
| 828 | |
| 829 | return error; |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * gfs2_follow_link - Follow a symbolic link |
| 834 | * @dentry: The dentry of the link |
| 835 | * @nd: Data that we pass to vfs_follow_link() |
| 836 | * |
| 837 | * This can handle symlinks of any size. It is optimised for symlinks |
| 838 | * under GFS2_FAST_NAME_SIZE. |
| 839 | * |
| 840 | * Returns: 0 on success or error code |
| 841 | */ |
| 842 | |
| 843 | static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 844 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 845 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 846 | char array[GFS2_FAST_NAME_SIZE], *buf = array; |
| 847 | unsigned int len = GFS2_FAST_NAME_SIZE; |
| 848 | int error; |
| 849 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 850 | error = gfs2_readlinki(ip, &buf, &len); |
| 851 | if (!error) { |
| 852 | error = vfs_follow_link(nd, buf); |
| 853 | if (buf != array) |
| 854 | kfree(buf); |
| 855 | } |
| 856 | |
| 857 | return ERR_PTR(error); |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * gfs2_permission - |
| 862 | * @inode: |
| 863 | * @mask: |
| 864 | * @nd: passed from Linux VFS, ignored by us |
| 865 | * |
Steven Whitehouse | 300c7d7 | 2006-11-27 09:55:28 -0500 | [diff] [blame] | 866 | * This may be called from the VFS directly, or from within GFS2 with the |
| 867 | * inode locked, so we look to see if the glock is already locked and only |
| 868 | * lock the glock if its not already been done. |
| 869 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 870 | * Returns: errno |
| 871 | */ |
| 872 | |
| 873 | static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd) |
| 874 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 875 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 876 | struct gfs2_holder i_gh; |
| 877 | int error; |
Steven Whitehouse | 300c7d7 | 2006-11-27 09:55:28 -0500 | [diff] [blame] | 878 | int unlock = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 879 | |
Steven Whitehouse | 300c7d7 | 2006-11-27 09:55:28 -0500 | [diff] [blame] | 880 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) { |
| 881 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
| 882 | if (error) |
| 883 | return error; |
| 884 | unlock = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Steven Whitehouse | 77386e1 | 2006-11-29 10:41:49 -0500 | [diff] [blame] | 887 | error = generic_permission(inode, mask, gfs2_check_acl); |
Steven Whitehouse | 300c7d7 | 2006-11-27 09:55:28 -0500 | [diff] [blame] | 888 | if (unlock) |
| 889 | gfs2_glock_dq_uninit(&i_gh); |
| 890 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 891 | return error; |
| 892 | } |
| 893 | |
| 894 | static int setattr_size(struct inode *inode, struct iattr *attr) |
| 895 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 896 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 897 | int error; |
| 898 | |
| 899 | if (attr->ia_size != ip->i_di.di_size) { |
| 900 | error = vmtruncate(inode, attr->ia_size); |
| 901 | if (error) |
| 902 | return error; |
| 903 | } |
| 904 | |
Steven Whitehouse | aa6a85a | 2006-01-24 10:37:06 +0000 | [diff] [blame] | 905 | error = gfs2_truncatei(ip, attr->ia_size); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 906 | if (error) |
| 907 | return error; |
| 908 | |
| 909 | return error; |
| 910 | } |
| 911 | |
| 912 | static int setattr_chown(struct inode *inode, struct iattr *attr) |
| 913 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 914 | struct gfs2_inode *ip = GFS2_I(inode); |
| 915 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 916 | struct buffer_head *dibh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 917 | u32 ouid, ogid, nuid, ngid; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 918 | int error; |
| 919 | |
Steven Whitehouse | 2933f92 | 2006-11-01 13:23:29 -0500 | [diff] [blame] | 920 | ouid = inode->i_uid; |
| 921 | ogid = inode->i_gid; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 922 | nuid = attr->ia_uid; |
| 923 | ngid = attr->ia_gid; |
| 924 | |
| 925 | if (!(attr->ia_valid & ATTR_UID) || ouid == nuid) |
| 926 | ouid = nuid = NO_QUOTA_CHANGE; |
| 927 | if (!(attr->ia_valid & ATTR_GID) || ogid == ngid) |
| 928 | ogid = ngid = NO_QUOTA_CHANGE; |
| 929 | |
| 930 | gfs2_alloc_get(ip); |
| 931 | |
| 932 | error = gfs2_quota_lock(ip, nuid, ngid); |
| 933 | if (error) |
| 934 | goto out_alloc; |
| 935 | |
| 936 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { |
| 937 | error = gfs2_quota_check(ip, nuid, ngid); |
| 938 | if (error) |
| 939 | goto out_gunlock_q; |
| 940 | } |
| 941 | |
| 942 | error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0); |
| 943 | if (error) |
| 944 | goto out_gunlock_q; |
| 945 | |
| 946 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 947 | if (error) |
| 948 | goto out_end_trans; |
| 949 | |
| 950 | error = inode_setattr(inode, attr); |
| 951 | gfs2_assert_warn(sdp, !error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 952 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 953 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 954 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 955 | brelse(dibh); |
| 956 | |
| 957 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { |
Steven Whitehouse | af18ddb | 2006-06-24 15:42:21 -0400 | [diff] [blame] | 958 | gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid); |
| 959 | gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 962 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 963 | gfs2_trans_end(sdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 964 | out_gunlock_q: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 965 | gfs2_quota_unlock(ip); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 966 | out_alloc: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 967 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 968 | return error; |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * gfs2_setattr - Change attributes on an inode |
| 973 | * @dentry: The dentry which is changing |
| 974 | * @attr: The structure describing the change |
| 975 | * |
| 976 | * The VFS layer wants to change one or more of an inodes attributes. Write |
| 977 | * that change out to disk. |
| 978 | * |
| 979 | * Returns: errno |
| 980 | */ |
| 981 | |
| 982 | static int gfs2_setattr(struct dentry *dentry, struct iattr *attr) |
| 983 | { |
| 984 | struct inode *inode = dentry->d_inode; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 985 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 986 | struct gfs2_holder i_gh; |
| 987 | int error; |
| 988 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 989 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
| 990 | if (error) |
| 991 | return error; |
| 992 | |
| 993 | error = -EPERM; |
| 994 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 995 | goto out; |
| 996 | |
| 997 | error = inode_change_ok(inode, attr); |
| 998 | if (error) |
| 999 | goto out; |
| 1000 | |
| 1001 | if (attr->ia_valid & ATTR_SIZE) |
| 1002 | error = setattr_size(inode, attr); |
| 1003 | else if (attr->ia_valid & (ATTR_UID | ATTR_GID)) |
| 1004 | error = setattr_chown(inode, attr); |
| 1005 | else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode)) |
| 1006 | error = gfs2_acl_chmod(ip, attr); |
| 1007 | else |
| 1008 | error = gfs2_setattr_simple(ip, attr); |
| 1009 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1010 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1011 | gfs2_glock_dq_uninit(&i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1012 | if (!error) |
| 1013 | mark_inode_dirty(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1014 | return error; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * gfs2_getattr - Read out an inode's attributes |
Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 1019 | * @mnt: The vfsmount the inode is being accessed from |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1020 | * @dentry: The dentry to stat |
| 1021 | * @stat: The inode's stats |
| 1022 | * |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1023 | * This may be called from the VFS directly, or from within GFS2 with the |
| 1024 | * inode locked, so we look to see if the glock is already locked and only |
| 1025 | * lock the glock if its not already been done. Note that its the NFS |
| 1026 | * readdirplus operation which causes this to be called (from filldir) |
| 1027 | * with the glock already held. |
| 1028 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1029 | * Returns: errno |
| 1030 | */ |
| 1031 | |
| 1032 | static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 1033 | struct kstat *stat) |
| 1034 | { |
| 1035 | struct inode *inode = dentry->d_inode; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1036 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1037 | struct gfs2_holder gh; |
| 1038 | int error; |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1039 | int unlock = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1040 | |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1041 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) { |
| 1042 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
| 1043 | if (error) |
| 1044 | return error; |
| 1045 | unlock = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1048 | generic_fillattr(inode, stat); |
Steven Whitehouse | d7c103d | 2007-01-25 17:14:59 +0000 | [diff] [blame] | 1049 | if (unlock) |
Steven Whitehouse | dcf3dd8 | 2006-11-27 10:12:05 -0500 | [diff] [blame] | 1050 | gfs2_glock_dq_uninit(&gh); |
| 1051 | |
| 1052 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | static int gfs2_setxattr(struct dentry *dentry, const char *name, |
| 1056 | const void *data, size_t size, int flags) |
| 1057 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1058 | struct inode *inode = dentry->d_inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1059 | struct gfs2_ea_request er; |
| 1060 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1061 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 1062 | er.er_type = gfs2_ea_name2type(name, &er.er_name); |
| 1063 | if (er.er_type == GFS2_EATYPE_UNUSED) |
| 1064 | return -EOPNOTSUPP; |
| 1065 | er.er_data = (char *)data; |
| 1066 | er.er_name_len = strlen(er.er_name); |
| 1067 | er.er_data_len = size; |
| 1068 | er.er_flags = flags; |
| 1069 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1070 | gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1071 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1072 | return gfs2_ea_set(GFS2_I(inode), &er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name, |
| 1076 | void *data, size_t size) |
| 1077 | { |
| 1078 | struct gfs2_ea_request er; |
| 1079 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1080 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 1081 | er.er_type = gfs2_ea_name2type(name, &er.er_name); |
| 1082 | if (er.er_type == GFS2_EATYPE_UNUSED) |
| 1083 | return -EOPNOTSUPP; |
| 1084 | er.er_data = data; |
| 1085 | er.er_name_len = strlen(er.er_name); |
| 1086 | er.er_data_len = size; |
| 1087 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1088 | return gfs2_ea_get(GFS2_I(dentry->d_inode), &er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size) |
| 1092 | { |
| 1093 | struct gfs2_ea_request er; |
| 1094 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1095 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 1096 | er.er_data = (size) ? buffer : NULL; |
| 1097 | er.er_data_len = size; |
| 1098 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1099 | return gfs2_ea_list(GFS2_I(dentry->d_inode), &er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | static int gfs2_removexattr(struct dentry *dentry, const char *name) |
| 1103 | { |
| 1104 | struct gfs2_ea_request er; |
| 1105 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1106 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 1107 | er.er_type = gfs2_ea_name2type(name, &er.er_name); |
| 1108 | if (er.er_type == GFS2_EATYPE_UNUSED) |
| 1109 | return -EOPNOTSUPP; |
| 1110 | er.er_name_len = strlen(er.er_name); |
| 1111 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1112 | return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1115 | const struct inode_operations gfs2_file_iops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1116 | .permission = gfs2_permission, |
| 1117 | .setattr = gfs2_setattr, |
| 1118 | .getattr = gfs2_getattr, |
| 1119 | .setxattr = gfs2_setxattr, |
| 1120 | .getxattr = gfs2_getxattr, |
| 1121 | .listxattr = gfs2_listxattr, |
| 1122 | .removexattr = gfs2_removexattr, |
| 1123 | }; |
| 1124 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1125 | const struct inode_operations gfs2_dev_iops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1126 | .permission = gfs2_permission, |
| 1127 | .setattr = gfs2_setattr, |
| 1128 | .getattr = gfs2_getattr, |
| 1129 | .setxattr = gfs2_setxattr, |
| 1130 | .getxattr = gfs2_getxattr, |
| 1131 | .listxattr = gfs2_listxattr, |
| 1132 | .removexattr = gfs2_removexattr, |
| 1133 | }; |
| 1134 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1135 | const struct inode_operations gfs2_dir_iops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1136 | .create = gfs2_create, |
| 1137 | .lookup = gfs2_lookup, |
| 1138 | .link = gfs2_link, |
| 1139 | .unlink = gfs2_unlink, |
| 1140 | .symlink = gfs2_symlink, |
| 1141 | .mkdir = gfs2_mkdir, |
| 1142 | .rmdir = gfs2_rmdir, |
| 1143 | .mknod = gfs2_mknod, |
| 1144 | .rename = gfs2_rename, |
| 1145 | .permission = gfs2_permission, |
| 1146 | .setattr = gfs2_setattr, |
| 1147 | .getattr = gfs2_getattr, |
| 1148 | .setxattr = gfs2_setxattr, |
| 1149 | .getxattr = gfs2_getxattr, |
| 1150 | .listxattr = gfs2_listxattr, |
| 1151 | .removexattr = gfs2_removexattr, |
| 1152 | }; |
| 1153 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1154 | const struct inode_operations gfs2_symlink_iops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1155 | .readlink = gfs2_readlink, |
| 1156 | .follow_link = gfs2_follow_link, |
| 1157 | .permission = gfs2_permission, |
| 1158 | .setattr = gfs2_setattr, |
| 1159 | .getattr = gfs2_getattr, |
| 1160 | .setxattr = gfs2_setxattr, |
| 1161 | .getxattr = gfs2_getxattr, |
| 1162 | .listxattr = gfs2_listxattr, |
| 1163 | .removexattr = gfs2_removexattr, |
| 1164 | }; |
| 1165 | |