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/posix_acl.h> |
| 16 | #include <linux/sort.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 17 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 18 | #include <linux/crc32.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 19 | #include <linux/lm_interface.h> |
Ryan O'Hara | fcb47e0b | 2006-10-03 11:57:35 -0400 | [diff] [blame] | 20 | #include <linux/security.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 21 | |
| 22 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 23 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include "acl.h" |
| 25 | #include "bmap.h" |
| 26 | #include "dir.h" |
| 27 | #include "eattr.h" |
| 28 | #include "glock.h" |
| 29 | #include "glops.h" |
| 30 | #include "inode.h" |
| 31 | #include "log.h" |
| 32 | #include "meta_io.h" |
| 33 | #include "ops_address.h" |
| 34 | #include "ops_file.h" |
| 35 | #include "ops_inode.h" |
| 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 | /** |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 42 | * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 43 | * @ip: The GFS2 inode (with embedded disk inode data) |
| 44 | * @inode: The Linux VFS inode |
| 45 | * |
| 46 | */ |
| 47 | |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 48 | void gfs2_inode_attr_in(struct gfs2_inode *ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 49 | { |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 50 | struct inode *inode = &ip->i_inode; |
Al Viro | 3ca68df | 2006-10-13 20:11:25 -0400 | [diff] [blame] | 51 | struct gfs2_dinode_host *di = &ip->i_di; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 52 | |
| 53 | inode->i_ino = ip->i_num.no_addr; |
Steven Whitehouse | c266871 | 2006-09-04 13:55:48 -0400 | [diff] [blame] | 54 | inode->i_nlink = di->di_nlink; |
| 55 | inode->i_uid = di->di_uid; |
| 56 | inode->i_gid = di->di_gid; |
| 57 | i_size_write(inode, di->di_size); |
| 58 | inode->i_atime.tv_sec = di->di_atime; |
| 59 | inode->i_mtime.tv_sec = di->di_mtime; |
| 60 | inode->i_ctime.tv_sec = di->di_ctime; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 61 | inode->i_atime.tv_nsec = 0; |
| 62 | inode->i_mtime.tv_nsec = 0; |
| 63 | inode->i_ctime.tv_nsec = 0; |
Steven Whitehouse | c266871 | 2006-09-04 13:55:48 -0400 | [diff] [blame] | 64 | inode->i_blocks = di->di_blocks << |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 65 | (GFS2_SB(inode)->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 66 | |
Steven Whitehouse | c266871 | 2006-09-04 13:55:48 -0400 | [diff] [blame] | 67 | if (di->di_flags & GFS2_DIF_IMMUTABLE) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 68 | inode->i_flags |= S_IMMUTABLE; |
| 69 | else |
| 70 | inode->i_flags &= ~S_IMMUTABLE; |
| 71 | |
Steven Whitehouse | c266871 | 2006-09-04 13:55:48 -0400 | [diff] [blame] | 72 | if (di->di_flags & GFS2_DIF_APPENDONLY) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 73 | inode->i_flags |= S_APPEND; |
| 74 | else |
| 75 | inode->i_flags &= ~S_APPEND; |
| 76 | } |
| 77 | |
| 78 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 79 | * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode |
| 80 | * @ip: The GFS2 inode |
| 81 | * |
| 82 | * Only copy out the attributes that we want the VFS layer |
| 83 | * to be able to modify. |
| 84 | */ |
| 85 | |
| 86 | void gfs2_inode_attr_out(struct gfs2_inode *ip) |
| 87 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 88 | struct inode *inode = &ip->i_inode; |
Al Viro | 3ca68df | 2006-10-13 20:11:25 -0400 | [diff] [blame] | 89 | struct gfs2_dinode_host *di = &ip->i_di; |
Steven Whitehouse | 75d3b81 | 2006-09-04 11:41:31 -0400 | [diff] [blame] | 90 | di->di_uid = inode->i_uid; |
| 91 | di->di_gid = inode->i_gid; |
| 92 | di->di_atime = inode->i_atime.tv_sec; |
| 93 | di->di_mtime = inode->i_mtime.tv_sec; |
| 94 | di->di_ctime = inode->i_ctime.tv_sec; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 97 | static int iget_test(struct inode *inode, void *opaque) |
| 98 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 99 | struct gfs2_inode *ip = GFS2_I(inode); |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 100 | struct gfs2_inum_host *inum = opaque; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 101 | |
| 102 | if (ip && ip->i_num.no_addr == inum->no_addr) |
| 103 | return 1; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 108 | static int iget_set(struct inode *inode, void *opaque) |
| 109 | { |
| 110 | struct gfs2_inode *ip = GFS2_I(inode); |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 111 | struct gfs2_inum_host *inum = opaque; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 112 | |
| 113 | ip->i_num = *inum; |
| 114 | return 0; |
| 115 | } |
| 116 | |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 117 | struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 118 | { |
| 119 | return ilookup5(sb, (unsigned long)inum->no_formal_ino, |
| 120 | iget_test, inum); |
| 121 | } |
| 122 | |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 123 | static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 124 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 125 | return iget5_locked(sb, (unsigned long)inum->no_formal_ino, |
| 126 | iget_test, iget_set, inum); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * gfs2_inode_lookup - Lookup an inode |
| 131 | * @sb: The super block |
| 132 | * @inum: The inode number |
| 133 | * @type: The type of the inode |
| 134 | * |
| 135 | * Returns: A VFS inode, or an error |
| 136 | */ |
| 137 | |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 138 | struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type) |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 139 | { |
| 140 | struct inode *inode = gfs2_iget(sb, inum); |
| 141 | struct gfs2_inode *ip = GFS2_I(inode); |
| 142 | struct gfs2_glock *io_gl; |
| 143 | int error; |
| 144 | |
Steven Whitehouse | 26d83de | 2006-10-30 16:59:08 -0500 | [diff] [blame] | 145 | if (!inode) |
| 146 | return ERR_PTR(-ENOBUFS); |
| 147 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 148 | if (inode->i_state & I_NEW) { |
| 149 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 150 | umode_t mode = DT2IF(type); |
Theodore Ts'o | bba9dfd | 2006-09-27 01:50:31 -0700 | [diff] [blame] | 151 | inode->i_private = ip; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 152 | inode->i_mode = mode; |
| 153 | |
| 154 | if (S_ISREG(mode)) { |
| 155 | inode->i_op = &gfs2_file_iops; |
| 156 | inode->i_fop = &gfs2_file_fops; |
| 157 | inode->i_mapping->a_ops = &gfs2_file_aops; |
| 158 | } else if (S_ISDIR(mode)) { |
| 159 | inode->i_op = &gfs2_dir_iops; |
| 160 | inode->i_fop = &gfs2_dir_fops; |
| 161 | } else if (S_ISLNK(mode)) { |
| 162 | inode->i_op = &gfs2_symlink_iops; |
| 163 | } else { |
| 164 | inode->i_op = &gfs2_dev_iops; |
| 165 | } |
| 166 | |
| 167 | error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl); |
| 168 | if (unlikely(error)) |
| 169 | goto fail; |
| 170 | ip->i_gl->gl_object = ip; |
| 171 | |
| 172 | error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl); |
| 173 | if (unlikely(error)) |
| 174 | goto fail_put; |
| 175 | |
| 176 | ip->i_vn = ip->i_gl->gl_vn - 1; |
| 177 | error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh); |
| 178 | if (unlikely(error)) |
| 179 | goto fail_iopen; |
| 180 | |
| 181 | gfs2_glock_put(io_gl); |
| 182 | unlock_new_inode(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 183 | } |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 184 | |
| 185 | return inode; |
| 186 | fail_iopen: |
| 187 | gfs2_glock_put(io_gl); |
| 188 | fail_put: |
| 189 | ip->i_gl->gl_object = NULL; |
| 190 | gfs2_glock_put(ip->i_gl); |
| 191 | fail: |
| 192 | iput(inode); |
| 193 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Steven Whitehouse | af339c0 | 2006-11-01 10:34:15 -0500 | [diff] [blame] | 196 | static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf) |
Steven Whitehouse | ea744d0 | 2006-10-31 15:28:00 -0500 | [diff] [blame] | 197 | { |
| 198 | struct gfs2_dinode_host *di = &ip->i_di; |
| 199 | const struct gfs2_dinode *str = buf; |
| 200 | |
Steven Whitehouse | af339c0 | 2006-11-01 10:34:15 -0500 | [diff] [blame] | 201 | if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) { |
| 202 | if (gfs2_consist_inode(ip)) |
| 203 | gfs2_dinode_print(ip); |
| 204 | return -EIO; |
| 205 | } |
| 206 | if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino)) |
| 207 | return -ESTALE; |
Steven Whitehouse | ea744d0 | 2006-10-31 15:28:00 -0500 | [diff] [blame] | 208 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame^] | 209 | ip->i_inode.i_mode = be32_to_cpu(str->di_mode); |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 210 | ip->i_inode.i_rdev = 0; |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame^] | 211 | switch (ip->i_inode.i_mode & S_IFMT) { |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 212 | case S_IFBLK: |
| 213 | case S_IFCHR: |
| 214 | ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major), |
| 215 | be32_to_cpu(str->di_minor)); |
| 216 | break; |
| 217 | }; |
| 218 | |
Steven Whitehouse | ea744d0 | 2006-10-31 15:28:00 -0500 | [diff] [blame] | 219 | di->di_uid = be32_to_cpu(str->di_uid); |
| 220 | di->di_gid = be32_to_cpu(str->di_gid); |
| 221 | di->di_nlink = be32_to_cpu(str->di_nlink); |
| 222 | di->di_size = be64_to_cpu(str->di_size); |
| 223 | di->di_blocks = be64_to_cpu(str->di_blocks); |
| 224 | di->di_atime = be64_to_cpu(str->di_atime); |
| 225 | di->di_mtime = be64_to_cpu(str->di_mtime); |
| 226 | di->di_ctime = be64_to_cpu(str->di_ctime); |
Steven Whitehouse | ea744d0 | 2006-10-31 15:28:00 -0500 | [diff] [blame] | 227 | |
| 228 | di->di_goal_meta = be64_to_cpu(str->di_goal_meta); |
| 229 | di->di_goal_data = be64_to_cpu(str->di_goal_data); |
| 230 | di->di_generation = be64_to_cpu(str->di_generation); |
| 231 | |
| 232 | di->di_flags = be32_to_cpu(str->di_flags); |
| 233 | di->di_payload_format = be32_to_cpu(str->di_payload_format); |
| 234 | di->di_height = be16_to_cpu(str->di_height); |
| 235 | |
| 236 | di->di_depth = be16_to_cpu(str->di_depth); |
| 237 | di->di_entries = be32_to_cpu(str->di_entries); |
| 238 | |
| 239 | di->di_eattr = be64_to_cpu(str->di_eattr); |
Steven Whitehouse | af339c0 | 2006-11-01 10:34:15 -0500 | [diff] [blame] | 240 | return 0; |
Steven Whitehouse | ea744d0 | 2006-10-31 15:28:00 -0500 | [diff] [blame] | 241 | } |
| 242 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 243 | /** |
| 244 | * gfs2_inode_refresh - Refresh the incore copy of the dinode |
| 245 | * @ip: The GFS2 inode |
| 246 | * |
| 247 | * Returns: errno |
| 248 | */ |
| 249 | |
| 250 | int gfs2_inode_refresh(struct gfs2_inode *ip) |
| 251 | { |
| 252 | struct buffer_head *dibh; |
| 253 | int error; |
| 254 | |
| 255 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 256 | if (error) |
| 257 | return error; |
| 258 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 259 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 260 | brelse(dibh); |
| 261 | return -EIO; |
| 262 | } |
| 263 | |
Steven Whitehouse | af339c0 | 2006-11-01 10:34:15 -0500 | [diff] [blame] | 264 | error = gfs2_dinode_in(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 265 | brelse(dibh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 266 | ip->i_vn = ip->i_gl->gl_vn; |
| 267 | |
Steven Whitehouse | af339c0 | 2006-11-01 10:34:15 -0500 | [diff] [blame] | 268 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 271 | int gfs2_dinode_dealloc(struct gfs2_inode *ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 272 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 273 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 274 | struct gfs2_alloc *al; |
| 275 | struct gfs2_rgrpd *rgd; |
| 276 | int error; |
| 277 | |
| 278 | if (ip->i_di.di_blocks != 1) { |
| 279 | if (gfs2_consist_inode(ip)) |
Steven Whitehouse | 4cc14f0 | 2006-10-31 19:00:24 -0500 | [diff] [blame] | 280 | gfs2_dinode_print(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 281 | return -EIO; |
| 282 | } |
| 283 | |
| 284 | al = gfs2_alloc_get(ip); |
| 285 | |
| 286 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 287 | if (error) |
| 288 | goto out; |
| 289 | |
| 290 | error = gfs2_rindex_hold(sdp, &al->al_ri_gh); |
| 291 | if (error) |
| 292 | goto out_qs; |
| 293 | |
| 294 | rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr); |
| 295 | if (!rgd) { |
| 296 | gfs2_consist_inode(ip); |
| 297 | error = -EIO; |
| 298 | goto out_rindex_relse; |
| 299 | } |
| 300 | |
| 301 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, |
| 302 | &al->al_rgd_gh); |
| 303 | if (error) |
| 304 | goto out_rindex_relse; |
| 305 | |
Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 306 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 307 | if (error) |
| 308 | goto out_rg_gunlock; |
| 309 | |
| 310 | gfs2_trans_add_gl(ip->i_gl); |
| 311 | |
| 312 | gfs2_free_di(rgd, ip); |
| 313 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 314 | gfs2_trans_end(sdp); |
| 315 | clear_bit(GLF_STICKY, &ip->i_gl->gl_flags); |
| 316 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 317 | out_rg_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 318 | gfs2_glock_dq_uninit(&al->al_rgd_gh); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 319 | out_rindex_relse: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 320 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 321 | out_qs: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 322 | gfs2_quota_unhold(ip); |
Steven Whitehouse | 3632752 | 2006-04-28 10:46:21 -0400 | [diff] [blame] | 323 | out: |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 324 | gfs2_alloc_put(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 325 | return error; |
| 326 | } |
| 327 | |
| 328 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 329 | * gfs2_change_nlink - Change nlink count on inode |
| 330 | * @ip: The GFS2 inode |
| 331 | * @diff: The change in the nlink count required |
| 332 | * |
| 333 | * Returns: errno |
| 334 | */ |
| 335 | |
| 336 | int gfs2_change_nlink(struct gfs2_inode *ip, int diff) |
| 337 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 338 | struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 339 | struct buffer_head *dibh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 340 | u32 nlink; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 341 | int error; |
| 342 | |
Steven Whitehouse | 29937ac | 2006-07-06 17:58:03 -0400 | [diff] [blame] | 343 | BUG_ON(ip->i_di.di_nlink != ip->i_inode.i_nlink); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 344 | nlink = ip->i_di.di_nlink + diff; |
| 345 | |
| 346 | /* If we are reducing the nlink count, but the new value ends up being |
| 347 | bigger than the old one, we must have underflowed. */ |
| 348 | if (diff < 0 && nlink > ip->i_di.di_nlink) { |
| 349 | if (gfs2_consist_inode(ip)) |
Steven Whitehouse | 4cc14f0 | 2006-10-31 19:00:24 -0500 | [diff] [blame] | 350 | gfs2_dinode_print(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 351 | return -EIO; |
| 352 | } |
| 353 | |
| 354 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 355 | if (error) |
| 356 | return error; |
| 357 | |
| 358 | ip->i_di.di_nlink = nlink; |
| 359 | ip->i_di.di_ctime = get_seconds(); |
Steven Whitehouse | 29937ac | 2006-07-06 17:58:03 -0400 | [diff] [blame] | 360 | ip->i_inode.i_nlink = nlink; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 361 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 362 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 363 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 364 | brelse(dibh); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 365 | mark_inode_dirty(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 366 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 367 | if (ip->i_di.di_nlink == 0) { |
| 368 | struct gfs2_rgrpd *rgd; |
| 369 | struct gfs2_holder ri_gh, rg_gh; |
| 370 | |
| 371 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 372 | if (error) |
| 373 | goto out; |
| 374 | error = -EIO; |
| 375 | rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr); |
| 376 | if (!rgd) |
| 377 | goto out_norgrp; |
| 378 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh); |
| 379 | if (error) |
| 380 | goto out_norgrp; |
| 381 | |
Steven Whitehouse | f92a0b6 | 2006-10-02 16:01:53 -0400 | [diff] [blame] | 382 | clear_nlink(&ip->i_inode); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 383 | gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */ |
| 384 | gfs2_glock_dq_uninit(&rg_gh); |
| 385 | out_norgrp: |
| 386 | gfs2_glock_dq_uninit(&ri_gh); |
| 387 | } |
| 388 | out: |
| 389 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 392 | struct inode *gfs2_lookup_simple(struct inode *dip, const char *name) |
| 393 | { |
| 394 | struct qstr qstr; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 395 | gfs2_str2qstr(&qstr, name); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 396 | return gfs2_lookupi(dip, &qstr, 1, NULL); |
| 397 | } |
| 398 | |
| 399 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 400 | /** |
| 401 | * gfs2_lookupi - Look up a filename in a directory and return its inode |
| 402 | * @d_gh: An initialized holder for the directory glock |
| 403 | * @name: The name of the inode to look for |
| 404 | * @is_root: If 1, ignore the caller's permissions |
| 405 | * @i_gh: An uninitialized holder for the new inode glock |
| 406 | * |
| 407 | * There will always be a vnode (Linux VFS inode) for the d_gh inode unless |
| 408 | * @is_root is true. |
| 409 | * |
| 410 | * Returns: errno |
| 411 | */ |
| 412 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 413 | struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name, |
| 414 | int is_root, struct nameidata *nd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 415 | { |
Steven Whitehouse | c9fd430 | 2006-03-01 15:31:02 -0500 | [diff] [blame] | 416 | struct super_block *sb = dir->i_sb; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 417 | struct gfs2_inode *dip = GFS2_I(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 418 | struct gfs2_holder d_gh; |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 419 | struct gfs2_inum_host inum; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 420 | unsigned int type; |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 421 | int error = 0; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 422 | struct inode *inode = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 423 | |
| 424 | if (!name->len || name->len > GFS2_FNAMESIZE) |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 425 | return ERR_PTR(-ENAMETOOLONG); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 426 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 427 | if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) || |
| 428 | (name->len == 2 && memcmp(name->name, "..", 2) == 0 && |
| 429 | dir == sb->s_root->d_inode)) { |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 430 | igrab(dir); |
| 431 | return dir; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); |
| 435 | if (error) |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 436 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 437 | |
| 438 | if (!is_root) { |
Steven Whitehouse | faf450e | 2006-06-22 10:59:10 -0400 | [diff] [blame] | 439 | error = permission(dir, MAY_EXEC, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 440 | if (error) |
| 441 | goto out; |
| 442 | } |
| 443 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 444 | error = gfs2_dir_search(dir, name, &inum, &type); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 445 | if (error) |
| 446 | goto out; |
| 447 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 448 | inode = gfs2_inode_lookup(sb, &inum, type); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 449 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 450 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 451 | gfs2_glock_dq_uninit(&d_gh); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 452 | if (error == -ENOENT) |
| 453 | return NULL; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 454 | return inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 457 | static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 458 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 459 | struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 460 | struct buffer_head *bh; |
Al Viro | e697264 | 2006-10-13 21:29:46 -0400 | [diff] [blame] | 461 | struct gfs2_inum_range_host ir; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 462 | int error; |
| 463 | |
| 464 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); |
| 465 | if (error) |
| 466 | return error; |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 467 | mutex_lock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 468 | |
| 469 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 470 | if (error) { |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 471 | mutex_unlock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 472 | gfs2_trans_end(sdp); |
| 473 | return error; |
| 474 | } |
| 475 | |
| 476 | gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
| 477 | |
| 478 | if (ir.ir_length) { |
| 479 | *formal_ino = ir.ir_start++; |
| 480 | ir.ir_length--; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 481 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 482 | gfs2_inum_range_out(&ir, |
| 483 | bh->b_data + sizeof(struct gfs2_dinode)); |
| 484 | brelse(bh); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 485 | mutex_unlock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 486 | gfs2_trans_end(sdp); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | brelse(bh); |
| 491 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 492 | mutex_unlock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 493 | gfs2_trans_end(sdp); |
| 494 | |
| 495 | return 1; |
| 496 | } |
| 497 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 498 | static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 499 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 500 | struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode); |
| 501 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 502 | struct gfs2_holder gh; |
| 503 | struct buffer_head *bh; |
Al Viro | e697264 | 2006-10-13 21:29:46 -0400 | [diff] [blame] | 504 | struct gfs2_inum_range_host ir; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 505 | int error; |
| 506 | |
| 507 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 508 | if (error) |
| 509 | return error; |
| 510 | |
| 511 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0); |
| 512 | if (error) |
| 513 | goto out; |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 514 | mutex_lock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 515 | |
| 516 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 517 | if (error) |
| 518 | goto out_end_trans; |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 519 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 520 | gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
| 521 | |
| 522 | if (!ir.ir_length) { |
| 523 | struct buffer_head *m_bh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 524 | u64 x, y; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 525 | __be64 z; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 526 | |
| 527 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); |
| 528 | if (error) |
| 529 | goto out_brelse; |
| 530 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 531 | z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)); |
| 532 | x = y = be64_to_cpu(z); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 533 | ir.ir_start = x; |
| 534 | ir.ir_length = GFS2_INUM_QUANTUM; |
| 535 | x += GFS2_INUM_QUANTUM; |
| 536 | if (x < y) |
| 537 | gfs2_consist_inode(m_ip); |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 538 | z = cpu_to_be64(x); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 539 | gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1); |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 540 | *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 541 | |
| 542 | brelse(m_bh); |
| 543 | } |
| 544 | |
| 545 | *formal_ino = ir.ir_start++; |
| 546 | ir.ir_length--; |
| 547 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 548 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 549 | gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
| 550 | |
Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 551 | out_brelse: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 552 | brelse(bh); |
Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 553 | out_end_trans: |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 554 | mutex_unlock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 555 | gfs2_trans_end(sdp); |
Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 556 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 557 | gfs2_glock_dq_uninit(&gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 558 | return error; |
| 559 | } |
| 560 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 561 | static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 562 | { |
| 563 | int error; |
| 564 | |
| 565 | error = pick_formal_ino_1(sdp, inum); |
| 566 | if (error <= 0) |
| 567 | return error; |
| 568 | |
| 569 | error = pick_formal_ino_2(sdp, inum); |
| 570 | |
| 571 | return error; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * create_ok - OK to create a new on-disk inode here? |
| 576 | * @dip: Directory in which dinode is to be created |
| 577 | * @name: Name of new dinode |
| 578 | * @mode: |
| 579 | * |
| 580 | * Returns: errno |
| 581 | */ |
| 582 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 583 | static int create_ok(struct gfs2_inode *dip, const struct qstr *name, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 584 | unsigned int mode) |
| 585 | { |
| 586 | int error; |
| 587 | |
Steven Whitehouse | faf450e | 2006-06-22 10:59:10 -0400 | [diff] [blame] | 588 | error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 589 | if (error) |
| 590 | return error; |
| 591 | |
| 592 | /* Don't create entries in an unlinked directory */ |
| 593 | if (!dip->i_di.di_nlink) |
| 594 | return -EPERM; |
| 595 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 596 | error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 597 | switch (error) { |
| 598 | case -ENOENT: |
| 599 | error = 0; |
| 600 | break; |
| 601 | case 0: |
| 602 | return -EEXIST; |
| 603 | default: |
| 604 | return error; |
| 605 | } |
| 606 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 607 | if (dip->i_di.di_entries == (u32)-1) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 608 | return -EFBIG; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 609 | if (S_ISDIR(mode) && dip->i_di.di_nlink == (u32)-1) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 610 | return -EMLINK; |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode, |
| 616 | unsigned int *uid, unsigned int *gid) |
| 617 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 618 | if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir && |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame^] | 619 | (dip->i_inode.i_mode & S_ISUID) && dip->i_di.di_uid) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 620 | if (S_ISDIR(*mode)) |
| 621 | *mode |= S_ISUID; |
| 622 | else if (dip->i_di.di_uid != current->fsuid) |
| 623 | *mode &= ~07111; |
| 624 | *uid = dip->i_di.di_uid; |
| 625 | } else |
| 626 | *uid = current->fsuid; |
| 627 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame^] | 628 | if (dip->i_inode.i_mode & S_ISGID) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 629 | if (S_ISDIR(*mode)) |
| 630 | *mode |= S_ISGID; |
| 631 | *gid = dip->i_di.di_gid; |
| 632 | } else |
| 633 | *gid = current->fsgid; |
| 634 | } |
| 635 | |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 636 | static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum, |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 637 | u64 *generation) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 638 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 639 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 640 | int error; |
| 641 | |
| 642 | gfs2_alloc_get(dip); |
| 643 | |
| 644 | dip->i_alloc.al_requested = RES_DINODE; |
| 645 | error = gfs2_inplace_reserve(dip); |
| 646 | if (error) |
| 647 | goto out; |
| 648 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 649 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 650 | if (error) |
| 651 | goto out_ipreserv; |
| 652 | |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 653 | inum->no_addr = gfs2_alloc_di(dip, generation); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 654 | |
| 655 | gfs2_trans_end(sdp); |
| 656 | |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 657 | out_ipreserv: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 658 | gfs2_inplace_release(dip); |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 659 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 660 | gfs2_alloc_put(dip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 661 | return error; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * init_dinode - Fill in a new dinode structure |
| 666 | * @dip: the directory this inode is being created in |
| 667 | * @gl: The glock covering the new inode |
| 668 | * @inum: the inode number |
| 669 | * @mode: the file permissions |
| 670 | * @uid: |
| 671 | * @gid: |
| 672 | * |
| 673 | */ |
| 674 | |
| 675 | static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 676 | const struct gfs2_inum_host *inum, unsigned int mode, |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 677 | unsigned int uid, unsigned int gid, |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 678 | const u64 *generation, dev_t dev) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 679 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 680 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 681 | struct gfs2_dinode *di; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 682 | struct buffer_head *dibh; |
| 683 | |
| 684 | dibh = gfs2_meta_new(gl, inum->no_addr); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 685 | gfs2_trans_add_bh(gl, dibh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 686 | gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI); |
| 687 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 688 | di = (struct gfs2_dinode *)dibh->b_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 689 | |
Steven Whitehouse | 2442a09 | 2006-01-30 11:49:32 +0000 | [diff] [blame] | 690 | di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino); |
| 691 | di->di_num.no_addr = cpu_to_be64(inum->no_addr); |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 692 | di->di_mode = cpu_to_be32(mode); |
| 693 | di->di_uid = cpu_to_be32(uid); |
| 694 | di->di_gid = cpu_to_be32(gid); |
| 695 | di->di_nlink = cpu_to_be32(0); |
| 696 | di->di_size = cpu_to_be64(0); |
| 697 | di->di_blocks = cpu_to_be64(1); |
| 698 | di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds()); |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 699 | di->di_major = cpu_to_be32(MAJOR(dev)); |
| 700 | di->di_minor = cpu_to_be32(MINOR(dev)); |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 701 | di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr); |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 702 | di->di_generation = cpu_to_be64(*generation); |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 703 | di->di_flags = cpu_to_be32(0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 704 | |
| 705 | if (S_ISREG(mode)) { |
| 706 | if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) || |
| 707 | gfs2_tune_get(sdp, gt_new_files_jdata)) |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 708 | di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 709 | if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) || |
| 710 | gfs2_tune_get(sdp, gt_new_files_directio)) |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 711 | di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 712 | } else if (S_ISDIR(mode)) { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 713 | di->di_flags |= cpu_to_be32(dip->i_di.di_flags & |
| 714 | GFS2_DIF_INHERIT_DIRECTIO); |
| 715 | di->di_flags |= cpu_to_be32(dip->i_di.di_flags & |
| 716 | GFS2_DIF_INHERIT_JDATA); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 719 | di->__pad1 = 0; |
Abhijith Das | b2a580d | 2006-07-10 12:36:12 -0500 | [diff] [blame] | 720 | di->di_payload_format = cpu_to_be32(0); |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 721 | di->di_height = cpu_to_be32(0); |
| 722 | di->__pad2 = 0; |
| 723 | di->__pad3 = 0; |
| 724 | di->di_depth = cpu_to_be16(0); |
| 725 | di->di_entries = cpu_to_be32(0); |
| 726 | memset(&di->__pad4, 0, sizeof(di->__pad4)); |
| 727 | di->di_eattr = cpu_to_be64(0); |
| 728 | memset(&di->di_reserved, 0, sizeof(di->di_reserved)); |
| 729 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 730 | brelse(dibh); |
| 731 | } |
| 732 | |
| 733 | static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 734 | unsigned int mode, const struct gfs2_inum_host *inum, |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 735 | const u64 *generation, dev_t dev) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 736 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 737 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 738 | unsigned int uid, gid; |
| 739 | int error; |
| 740 | |
| 741 | munge_mode_uid_gid(dip, &mode, &uid, &gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 742 | gfs2_alloc_get(dip); |
| 743 | |
| 744 | error = gfs2_quota_lock(dip, uid, gid); |
| 745 | if (error) |
| 746 | goto out; |
| 747 | |
| 748 | error = gfs2_quota_check(dip, uid, gid); |
| 749 | if (error) |
| 750 | goto out_quota; |
| 751 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 752 | error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 753 | if (error) |
| 754 | goto out_quota; |
| 755 | |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 756 | init_dinode(dip, gl, inum, mode, uid, gid, generation, dev); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 757 | gfs2_quota_change(dip, +1, uid, gid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 758 | gfs2_trans_end(sdp); |
| 759 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 760 | out_quota: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 761 | gfs2_quota_unlock(dip); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 762 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 763 | gfs2_alloc_put(dip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 764 | return error; |
| 765 | } |
| 766 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 767 | static int link_dinode(struct gfs2_inode *dip, const struct qstr *name, |
| 768 | struct gfs2_inode *ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 769 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 770 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 771 | struct gfs2_alloc *al; |
| 772 | int alloc_required; |
| 773 | struct buffer_head *dibh; |
| 774 | int error; |
| 775 | |
| 776 | al = gfs2_alloc_get(dip); |
| 777 | |
| 778 | error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 779 | if (error) |
| 780 | goto fail; |
| 781 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 782 | error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 783 | if (alloc_required < 0) |
| 784 | goto fail; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 785 | if (alloc_required) { |
| 786 | error = gfs2_quota_check(dip, dip->i_di.di_uid, |
| 787 | dip->i_di.di_gid); |
| 788 | if (error) |
| 789 | goto fail_quota_locks; |
| 790 | |
| 791 | al->al_requested = sdp->sd_max_dirres; |
| 792 | |
| 793 | error = gfs2_inplace_reserve(dip); |
| 794 | if (error) |
| 795 | goto fail_quota_locks; |
| 796 | |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 797 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 798 | al->al_rgd->rd_ri.ri_length + |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 799 | 2 * RES_DINODE + |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 800 | RES_STATFS + RES_QUOTA, 0); |
| 801 | if (error) |
| 802 | goto fail_ipreserv; |
| 803 | } else { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 804 | error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 805 | if (error) |
| 806 | goto fail_quota_locks; |
| 807 | } |
| 808 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame^] | 809 | error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 810 | if (error) |
| 811 | goto fail_end_trans; |
| 812 | |
| 813 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 814 | if (error) |
| 815 | goto fail_end_trans; |
| 816 | ip->i_di.di_nlink = 1; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 817 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 818 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 819 | brelse(dibh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 820 | return 0; |
| 821 | |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 822 | fail_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 823 | gfs2_trans_end(sdp); |
| 824 | |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 825 | fail_ipreserv: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 826 | if (dip->i_alloc.al_rgd) |
| 827 | gfs2_inplace_release(dip); |
| 828 | |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 829 | fail_quota_locks: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 830 | gfs2_quota_unlock(dip); |
| 831 | |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 832 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 833 | gfs2_alloc_put(dip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 834 | return error; |
| 835 | } |
| 836 | |
Ryan O'Hara | fcb47e0b | 2006-10-03 11:57:35 -0400 | [diff] [blame] | 837 | static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip) |
| 838 | { |
| 839 | int err; |
| 840 | size_t len; |
| 841 | void *value; |
| 842 | char *name; |
| 843 | struct gfs2_ea_request er; |
| 844 | |
| 845 | err = security_inode_init_security(&ip->i_inode, &dip->i_inode, |
| 846 | &name, &value, &len); |
| 847 | |
| 848 | if (err) { |
| 849 | if (err == -EOPNOTSUPP) |
| 850 | return 0; |
| 851 | return err; |
| 852 | } |
| 853 | |
| 854 | memset(&er, 0, sizeof(struct gfs2_ea_request)); |
| 855 | |
| 856 | er.er_type = GFS2_EATYPE_SECURITY; |
| 857 | er.er_name = name; |
| 858 | er.er_data = value; |
| 859 | er.er_name_len = strlen(name); |
| 860 | er.er_data_len = len; |
| 861 | |
| 862 | err = gfs2_ea_set_i(ip, &er); |
| 863 | |
| 864 | kfree(value); |
| 865 | kfree(name); |
| 866 | |
| 867 | return err; |
| 868 | } |
| 869 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 870 | /** |
| 871 | * gfs2_createi - Create a new inode |
| 872 | * @ghs: An array of two holders |
| 873 | * @name: The name of the new file |
| 874 | * @mode: the permissions on the new inode |
| 875 | * |
| 876 | * @ghs[0] is an initialized holder for the directory |
| 877 | * @ghs[1] is the holder for the inode lock |
| 878 | * |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 879 | * If the return value is not NULL, the glocks on both the directory and the new |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 880 | * file are held. A transaction has been started and an inplace reservation |
| 881 | * is held, as well. |
| 882 | * |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 883 | * Returns: An inode |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 884 | */ |
| 885 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 886 | struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name, |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 887 | unsigned int mode, dev_t dev) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 888 | { |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 889 | struct inode *inode; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 890 | struct gfs2_inode *dip = ghs->gh_gl->gl_object; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 891 | struct inode *dir = &dip->i_inode; |
| 892 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 893 | struct gfs2_inum_host inum; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 894 | int error; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 895 | u64 generation; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 896 | |
| 897 | if (!name->len || name->len > GFS2_FNAMESIZE) |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 898 | return ERR_PTR(-ENAMETOOLONG); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 899 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 900 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); |
| 901 | error = gfs2_glock_nq(ghs); |
| 902 | if (error) |
| 903 | goto fail; |
| 904 | |
| 905 | error = create_ok(dip, name, mode); |
| 906 | if (error) |
| 907 | goto fail_gunlock; |
| 908 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 909 | error = pick_formal_ino(sdp, &inum.no_formal_ino); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 910 | if (error) |
| 911 | goto fail_gunlock; |
| 912 | |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 913 | error = alloc_dinode(dip, &inum, &generation); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 914 | if (error) |
| 915 | goto fail_gunlock; |
| 916 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 917 | if (inum.no_addr < dip->i_num.no_addr) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 918 | gfs2_glock_dq(ghs); |
| 919 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 920 | error = gfs2_glock_nq_num(sdp, inum.no_addr, |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 921 | &gfs2_inode_glops, LM_ST_EXCLUSIVE, |
| 922 | GL_SKIP, ghs + 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 923 | if (error) { |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 924 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); |
| 928 | error = gfs2_glock_nq(ghs); |
| 929 | if (error) { |
| 930 | gfs2_glock_dq_uninit(ghs + 1); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 931 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | error = create_ok(dip, name, mode); |
| 935 | if (error) |
| 936 | goto fail_gunlock2; |
| 937 | } else { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 938 | error = gfs2_glock_nq_num(sdp, inum.no_addr, |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 939 | &gfs2_inode_glops, LM_ST_EXCLUSIVE, |
| 940 | GL_SKIP, ghs + 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 941 | if (error) |
| 942 | goto fail_gunlock; |
| 943 | } |
| 944 | |
Steven Whitehouse | e7f14f4 | 2006-10-31 21:45:08 -0500 | [diff] [blame] | 945 | error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 946 | if (error) |
| 947 | goto fail_gunlock2; |
| 948 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 949 | inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode)); |
| 950 | if (IS_ERR(inode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 951 | goto fail_gunlock2; |
| 952 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 953 | error = gfs2_inode_refresh(GFS2_I(inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 954 | if (error) |
| 955 | goto fail_iput; |
| 956 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 957 | error = gfs2_acl_create(dip, GFS2_I(inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 958 | if (error) |
| 959 | goto fail_iput; |
| 960 | |
Ryan O'Hara | fcb47e0b | 2006-10-03 11:57:35 -0400 | [diff] [blame] | 961 | error = gfs2_security_init(dip, GFS2_I(inode)); |
| 962 | if (error) |
| 963 | goto fail_iput; |
| 964 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 965 | error = link_dinode(dip, name, GFS2_I(inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 966 | if (error) |
| 967 | goto fail_iput; |
| 968 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 969 | if (!inode) |
| 970 | return ERR_PTR(-ENOMEM); |
| 971 | return inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 972 | |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 973 | fail_iput: |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 974 | iput(inode); |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 975 | fail_gunlock2: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 976 | gfs2_glock_dq_uninit(ghs + 1); |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 977 | fail_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 978 | gfs2_glock_dq(ghs); |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 979 | fail: |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 980 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 984 | * gfs2_rmdiri - Remove a directory |
| 985 | * @dip: The parent directory of the directory to be removed |
| 986 | * @name: The name of the directory to be removed |
| 987 | * @ip: The GFS2 inode of the directory to be removed |
| 988 | * |
| 989 | * Assumes Glocks on dip and ip are held |
| 990 | * |
| 991 | * Returns: errno |
| 992 | */ |
| 993 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 994 | int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name, |
| 995 | struct gfs2_inode *ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 996 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 997 | struct qstr dotname; |
| 998 | int error; |
| 999 | |
| 1000 | if (ip->i_di.di_entries != 2) { |
| 1001 | if (gfs2_consist_inode(ip)) |
Steven Whitehouse | 4cc14f0 | 2006-10-31 19:00:24 -0500 | [diff] [blame] | 1002 | gfs2_dinode_print(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1003 | return -EIO; |
| 1004 | } |
| 1005 | |
| 1006 | error = gfs2_dir_del(dip, name); |
| 1007 | if (error) |
| 1008 | return error; |
| 1009 | |
| 1010 | error = gfs2_change_nlink(dip, -1); |
| 1011 | if (error) |
| 1012 | return error; |
| 1013 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1014 | gfs2_str2qstr(&dotname, "."); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1015 | error = gfs2_dir_del(ip, &dotname); |
| 1016 | if (error) |
| 1017 | return error; |
| 1018 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1019 | gfs2_str2qstr(&dotname, ".."); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1020 | error = gfs2_dir_del(ip, &dotname); |
| 1021 | if (error) |
| 1022 | return error; |
| 1023 | |
| 1024 | error = gfs2_change_nlink(ip, -2); |
| 1025 | if (error) |
| 1026 | return error; |
| 1027 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1028 | return error; |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * gfs2_unlink_ok - check to see that a inode is still in a directory |
| 1033 | * @dip: the directory |
| 1034 | * @name: the name of the file |
| 1035 | * @ip: the inode |
| 1036 | * |
| 1037 | * Assumes that the lock on (at least) @dip is held. |
| 1038 | * |
| 1039 | * Returns: 0 if the parent/child relationship is correct, errno if it isn't |
| 1040 | */ |
| 1041 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1042 | int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1043 | struct gfs2_inode *ip) |
| 1044 | { |
Al Viro | 629a21e | 2006-10-13 22:51:24 -0400 | [diff] [blame] | 1045 | struct gfs2_inum_host inum; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1046 | unsigned int type; |
| 1047 | int error; |
| 1048 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1049 | if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1050 | return -EPERM; |
| 1051 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame^] | 1052 | if ((dip->i_inode.i_mode & S_ISVTX) && |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1053 | dip->i_di.di_uid != current->fsuid && |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1054 | ip->i_di.di_uid != current->fsuid && !capable(CAP_FOWNER)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1055 | return -EPERM; |
| 1056 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1057 | if (IS_APPEND(&dip->i_inode)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1058 | return -EPERM; |
| 1059 | |
Steven Whitehouse | faf450e | 2006-06-22 10:59:10 -0400 | [diff] [blame] | 1060 | error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1061 | if (error) |
| 1062 | return error; |
| 1063 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1064 | error = gfs2_dir_search(&dip->i_inode, name, &inum, &type); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1065 | if (error) |
| 1066 | return error; |
| 1067 | |
| 1068 | if (!gfs2_inum_equal(&inum, &ip->i_num)) |
| 1069 | return -ENOENT; |
| 1070 | |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame^] | 1071 | if (IF2DT(ip->i_inode.i_mode) != type) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1072 | gfs2_consist_inode(dip); |
| 1073 | return -EIO; |
| 1074 | } |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * gfs2_ok_to_move - check if it's ok to move a directory to another directory |
| 1081 | * @this: move this |
| 1082 | * @to: to here |
| 1083 | * |
| 1084 | * Follow @to back to the root and make sure we don't encounter @this |
| 1085 | * Assumes we already hold the rename lock. |
| 1086 | * |
| 1087 | * Returns: errno |
| 1088 | */ |
| 1089 | |
| 1090 | int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) |
| 1091 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1092 | struct inode *dir = &to->i_inode; |
Steven Whitehouse | c9fd430 | 2006-03-01 15:31:02 -0500 | [diff] [blame] | 1093 | struct super_block *sb = dir->i_sb; |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1094 | struct inode *tmp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1095 | struct qstr dotdot; |
| 1096 | int error = 0; |
| 1097 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1098 | gfs2_str2qstr(&dotdot, ".."); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1099 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1100 | igrab(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1101 | |
| 1102 | for (;;) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1103 | if (dir == &this->i_inode) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1104 | error = -EINVAL; |
| 1105 | break; |
| 1106 | } |
Steven Whitehouse | c9fd430 | 2006-03-01 15:31:02 -0500 | [diff] [blame] | 1107 | if (dir == sb->s_root->d_inode) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1108 | error = 0; |
| 1109 | break; |
| 1110 | } |
| 1111 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1112 | tmp = gfs2_lookupi(dir, &dotdot, 1, NULL); |
| 1113 | if (IS_ERR(tmp)) { |
| 1114 | error = PTR_ERR(tmp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1115 | break; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1116 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1117 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1118 | iput(dir); |
| 1119 | dir = tmp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1122 | iput(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1123 | |
| 1124 | return error; |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * gfs2_readlinki - return the contents of a symlink |
| 1129 | * @ip: the symlink's inode |
| 1130 | * @buf: a pointer to the buffer to be filled |
| 1131 | * @len: a pointer to the length of @buf |
| 1132 | * |
| 1133 | * If @buf is too small, a piece of memory is kmalloc()ed and needs |
| 1134 | * to be freed by the caller. |
| 1135 | * |
| 1136 | * Returns: errno |
| 1137 | */ |
| 1138 | |
| 1139 | int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len) |
| 1140 | { |
| 1141 | struct gfs2_holder i_gh; |
| 1142 | struct buffer_head *dibh; |
| 1143 | unsigned int x; |
| 1144 | int error; |
| 1145 | |
| 1146 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); |
| 1147 | error = gfs2_glock_nq_atime(&i_gh); |
| 1148 | if (error) { |
| 1149 | gfs2_holder_uninit(&i_gh); |
| 1150 | return error; |
| 1151 | } |
| 1152 | |
| 1153 | if (!ip->i_di.di_size) { |
| 1154 | gfs2_consist_inode(ip); |
| 1155 | error = -EIO; |
| 1156 | goto out; |
| 1157 | } |
| 1158 | |
| 1159 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1160 | if (error) |
| 1161 | goto out; |
| 1162 | |
| 1163 | x = ip->i_di.di_size + 1; |
| 1164 | if (x > *len) { |
| 1165 | *buf = kmalloc(x, GFP_KERNEL); |
| 1166 | if (!*buf) { |
| 1167 | error = -ENOMEM; |
| 1168 | goto out_brelse; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x); |
| 1173 | *len = x; |
| 1174 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1175 | out_brelse: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1176 | brelse(dibh); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1177 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1178 | gfs2_glock_dq_uninit(&i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1179 | return error; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and |
| 1184 | * conditionally update the inode's atime |
| 1185 | * @gh: the holder to acquire |
| 1186 | * |
| 1187 | * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap |
| 1188 | * Update if the difference between the current time and the inode's current |
| 1189 | * atime is greater than an interval specified at mount. |
| 1190 | * |
| 1191 | * Returns: errno |
| 1192 | */ |
| 1193 | |
| 1194 | int gfs2_glock_nq_atime(struct gfs2_holder *gh) |
| 1195 | { |
| 1196 | struct gfs2_glock *gl = gh->gh_gl; |
| 1197 | struct gfs2_sbd *sdp = gl->gl_sbd; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1198 | struct gfs2_inode *ip = gl->gl_object; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1199 | s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1200 | unsigned int state; |
| 1201 | int flags; |
| 1202 | int error; |
| 1203 | |
| 1204 | if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) || |
| 1205 | gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) || |
| 1206 | gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops)) |
| 1207 | return -EINVAL; |
| 1208 | |
| 1209 | state = gh->gh_state; |
| 1210 | flags = gh->gh_flags; |
| 1211 | |
| 1212 | error = gfs2_glock_nq(gh); |
| 1213 | if (error) |
| 1214 | return error; |
| 1215 | |
| 1216 | if (test_bit(SDF_NOATIME, &sdp->sd_flags) || |
| 1217 | (sdp->sd_vfs->s_flags & MS_RDONLY)) |
| 1218 | return 0; |
| 1219 | |
| 1220 | curtime = get_seconds(); |
| 1221 | if (curtime - ip->i_di.di_atime >= quantum) { |
| 1222 | gfs2_glock_dq(gh); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 1223 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY, |
| 1224 | gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1225 | error = gfs2_glock_nq(gh); |
| 1226 | if (error) |
| 1227 | return error; |
| 1228 | |
| 1229 | /* Verify that atime hasn't been updated while we were |
| 1230 | trying to get exclusive lock. */ |
| 1231 | |
| 1232 | curtime = get_seconds(); |
| 1233 | if (curtime - ip->i_di.di_atime >= quantum) { |
| 1234 | struct buffer_head *dibh; |
Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 1235 | struct gfs2_dinode *di; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1236 | |
| 1237 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); |
| 1238 | if (error == -EROFS) |
| 1239 | return 0; |
| 1240 | if (error) |
| 1241 | goto fail; |
| 1242 | |
| 1243 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1244 | if (error) |
| 1245 | goto fail_end_trans; |
| 1246 | |
| 1247 | ip->i_di.di_atime = curtime; |
| 1248 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1249 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 48516ce | 2006-10-02 12:39:19 -0400 | [diff] [blame] | 1250 | di = (struct gfs2_dinode *)dibh->b_data; |
| 1251 | di->di_atime = cpu_to_be64(ip->i_di.di_atime); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1252 | brelse(dibh); |
| 1253 | |
| 1254 | gfs2_trans_end(sdp); |
| 1255 | } |
| 1256 | |
| 1257 | /* If someone else has asked for the glock, |
| 1258 | unlock and let them have it. Then reacquire |
| 1259 | in the original state. */ |
| 1260 | if (gfs2_glock_is_blocking(gl)) { |
| 1261 | gfs2_glock_dq(gh); |
| 1262 | gfs2_holder_reinit(state, flags, gh); |
| 1263 | return gfs2_glock_nq(gh); |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | return 0; |
| 1268 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1269 | fail_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1270 | gfs2_trans_end(sdp); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1271 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1272 | gfs2_glock_dq(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1273 | return error; |
| 1274 | } |
| 1275 | |
| 1276 | /** |
| 1277 | * glock_compare_atime - Compare two struct gfs2_glock structures for sort |
| 1278 | * @arg_a: the first structure |
| 1279 | * @arg_b: the second structure |
| 1280 | * |
| 1281 | * Returns: 1 if A > B |
| 1282 | * -1 if A < B |
Steven Whitehouse | 75d3b81 | 2006-09-04 11:41:31 -0400 | [diff] [blame] | 1283 | * 0 if A == B |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1284 | */ |
| 1285 | |
| 1286 | static int glock_compare_atime(const void *arg_a, const void *arg_b) |
| 1287 | { |
Steven Whitehouse | 75d3b81 | 2006-09-04 11:41:31 -0400 | [diff] [blame] | 1288 | const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a; |
| 1289 | const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b; |
| 1290 | const struct lm_lockname *a = &gh_a->gh_gl->gl_name; |
| 1291 | const struct lm_lockname *b = &gh_b->gh_gl->gl_name; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1292 | |
| 1293 | if (a->ln_number > b->ln_number) |
Steven Whitehouse | 75d3b81 | 2006-09-04 11:41:31 -0400 | [diff] [blame] | 1294 | return 1; |
| 1295 | if (a->ln_number < b->ln_number) |
| 1296 | return -1; |
| 1297 | if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE) |
| 1298 | return 1; |
| 1299 | if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME)) |
| 1300 | return 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1301 | |
Steven Whitehouse | 75d3b81 | 2006-09-04 11:41:31 -0400 | [diff] [blame] | 1302 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an |
| 1307 | * atime update |
| 1308 | * @num_gh: the number of structures |
| 1309 | * @ghs: an array of struct gfs2_holder structures |
| 1310 | * |
| 1311 | * Returns: 0 on success (all glocks acquired), |
| 1312 | * errno on failure (no glocks acquired) |
| 1313 | */ |
| 1314 | |
| 1315 | int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1316 | { |
| 1317 | struct gfs2_holder **p; |
| 1318 | unsigned int x; |
| 1319 | int error = 0; |
| 1320 | |
| 1321 | if (!num_gh) |
| 1322 | return 0; |
| 1323 | |
| 1324 | if (num_gh == 1) { |
| 1325 | ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1326 | if (ghs->gh_flags & GL_ATIME) |
| 1327 | error = gfs2_glock_nq_atime(ghs); |
| 1328 | else |
| 1329 | error = gfs2_glock_nq(ghs); |
| 1330 | return error; |
| 1331 | } |
| 1332 | |
| 1333 | p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL); |
| 1334 | if (!p) |
| 1335 | return -ENOMEM; |
| 1336 | |
| 1337 | for (x = 0; x < num_gh; x++) |
| 1338 | p[x] = &ghs[x]; |
| 1339 | |
| 1340 | sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL); |
| 1341 | |
| 1342 | for (x = 0; x < num_gh; x++) { |
| 1343 | p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1344 | |
| 1345 | if (p[x]->gh_flags & GL_ATIME) |
| 1346 | error = gfs2_glock_nq_atime(p[x]); |
| 1347 | else |
| 1348 | error = gfs2_glock_nq(p[x]); |
| 1349 | |
| 1350 | if (error) { |
| 1351 | while (x--) |
| 1352 | gfs2_glock_dq(p[x]); |
| 1353 | break; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | kfree(p); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1358 | return error; |
| 1359 | } |
| 1360 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1361 | |
| 1362 | static int |
| 1363 | __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) |
| 1364 | { |
| 1365 | struct buffer_head *dibh; |
| 1366 | int error; |
| 1367 | |
| 1368 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1369 | if (!error) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1370 | error = inode_setattr(&ip->i_inode, attr); |
| 1371 | gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1372 | gfs2_inode_attr_out(ip); |
| 1373 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1374 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1375 | gfs2_dinode_out(ip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1376 | brelse(dibh); |
| 1377 | } |
| 1378 | return error; |
| 1379 | } |
| 1380 | |
| 1381 | /** |
| 1382 | * gfs2_setattr_simple - |
| 1383 | * @ip: |
| 1384 | * @attr: |
| 1385 | * |
| 1386 | * Called with a reference on the vnode. |
| 1387 | * |
| 1388 | * Returns: errno |
| 1389 | */ |
| 1390 | |
| 1391 | int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) |
| 1392 | { |
| 1393 | int error; |
| 1394 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1395 | if (current->journal_info) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1396 | return __gfs2_setattr_simple(ip, attr); |
| 1397 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1398 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1399 | if (error) |
| 1400 | return error; |
| 1401 | |
| 1402 | error = __gfs2_setattr_simple(ip, attr); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1403 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1404 | return error; |
| 1405 | } |
| 1406 | |