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