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