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