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